MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Kueri Mongo Distinct with Sum tidak berfungsi

Tahap $group memiliki bentuk prototipe berikut:

{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }

Bidang1 Anda adalah Jam dan akumulator1 adalah jumlah ,Oleh karena itu agregasi Anda harus seperti ini:

db.pms_teamleadtimesheets.aggregate(

  // Pipeline
  [
    // Stage 1
    {
      $match: {
      UserName:"Admin",
      FacilityID:"59a53f0c6077b2a029c52b7f",
      ProjectID:"5a042ba02af18ac8388bd3c0"
      }
    },

    // Stage 2
    {
      $group: {
         _id: { ProjectID: "$ProjectID",
            Task: "$Text", Phase: "$phase",
            Comments: "$Comments", 
            TaskType: "$TaskType", 
            Items: "$Items", 
            UserName: "$UserName", 
            IsBillable: "$IsBillable", 
            Date: "$StartDate"
            },
            Hours:{$sum:"$Hours" }
      }
    },

    // Stage 3
    {
      $project: {
       _id: 0,
       ProjectID: "$_id.ProjectID", 
       Phase: "$_id.Phase",
       Task: "$_id.Task",
       Comments: "$_id.Comments",
       TaskType: "$_id.TaskType",
       Items: "$_id.Items",
       UserName: "$_id.UserName",
       IsBillable: "$_id.IsBillable",
       Date: "$_id.Date",
       Hours:  { 
         $divide: [ "$Hours", 60 ] 
         } 
      }
    },

  ]
);

Output untuk dengan data pengujian yang diberikan:

{ 
    "ProjectID" : "5a042ba02af18ac8388bd3c0", 
    "Phase" : "Analysis", 
    "Task" : "Analysis", 
    "Comments" : "2", 
    "TaskType" : "DELIVERY", 
    "Items" : "CRI", 
    "UserName" : "Admin", 
    "IsBillable" : true, 
    "Date" : "29-05-2018", 
    "Hours" : 4.0
}
{ 
    "ProjectID" : "5a042ba02af18ac8388bd3c0", 
    "Phase" : "Analysis", 
    "Task" : "Analysis", 
    "Comments" : "8", 
    "TaskType" : "DELIVERY", 
    "Items" : "Others", 
    "UserName" : "Admin", 
    "IsBillable" : true, 
    "Date" : "28-05-2018", 
    "Hours" : 4.0
}
{ 
    "ProjectID" : "5a042ba02af18ac8388bd3c0", 
    "Phase" : "Analysis", 
    "Task" : "Analysis", 
    "Comments" : "4", 
    "TaskType" : "DELIVERY", 
    "Items" : "Others", 
    "UserName" : "Admin", 
    "IsBillable" : true, 
    "Date" : "28-05-2018", 
    "Hours" : 4.0
}

Baca lebih lanjut di sini




  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Bagaimana cara menyediakan pagination sisi server dengan NestJS?

  2. Cara Menghubungkan Ke Deployment MongoDB Anda Menggunakan Robo 3T GUI

  3. Di mana menulis dan menyimpan peta mongoDB/mengurangi fungsi dalam proyek java

  4. Haruskah saya mengubah nilai kacang mongo saat digunakan di server produksi?

  5. solr Data Import Handler untuk MongoDB