Mongo查询与Sum的区别不起作用

时间:2018-05-29 13:59:54

标签: mongodb mongoose mongodb-query

我在这里更新了我的问题。 这是输入数据,您可以使用此命令插入本地db:

db.pms_teamleadtimesheets.insertMany( [
      { "Text" : "Analysis",
    "Comments" : "4",
    "TaskType" : "DELIVERY",
    "Items" : "Others",
    "StartDate" : "28-05-2018",
    "EndDate" : "2018-05-28",
    "Hours" : 240,
    "phase" : "Analysis",
    "ProjectID" : "5a042ba02af18ac8388bd3c0",
    "UserName" : "Admin",
    "FacilityID" : "59a53f0c6077b2a029c52b7f",
    "TaskID" : "5b0baafffb8df2401af90fea",
    "TaskDescription" : "Analysis",
    "IsBillable" : true
     },
      {  "Text" : "Analysis",
    "Comments" : "8",
    "TaskType" : "DELIVERY",
    "Items" : "Others",
    "StartDate" : "28-05-2018",
    "EndDate" : "2018-05-28",
    "Hours" : 240,
    "phase" : "Analysis",
    "ProjectID" : "5a042ba02af18ac8388bd3c0",
    "UserName" : "Admin",
    "FacilityID" : "59a53f0c6077b2a029c52b7f",
    "TaskID" : "5b0baafffb8df2401af90fea",
    "TaskDescription" : "Analysis",
    "IsBillable" : true
   },
      {"Text" : "Analysis",
    "Comments" : "2",
    "TaskType" : "DELIVERY",
    "Items" : "CRI",
    "StartDate" : "29-05-2018",
    "EndDate" : "2018-05-29",
    "Hours" : 120,
    "phase" : "Analysis",
    "ProjectID" : "5a042ba02af18ac8388bd3c0",
    "UserName" : "Admin",
    "FacilityID" : "59a53f0c6077b2a029c52b7f",
    "TaskID" : "5b0baafffb8df2401af90fea",
    "TaskDescription" : "Analysis",
    "IsBillable" : true
    },
       { "Text" : "Analysis",
    "Comments" : "2",
    "TaskType" : "DELIVERY",
    "Items" : "CRI",
    "StartDate" : "29-05-2018",
    "EndDate" : "2018-05-29",
    "Hours" : 120,
    "phase" : "Analysis",
    "ProjectID" : "5a042ba02af18ac8388bd3c0",
    "UserName" : "Admin",
    "FacilityID" : "59a53f0c6077b2a029c52b7f",
    "TaskID" : "5b0baafffb8df2401af90fea",
    "TaskDescription" : "Analysis",
    "IsBillable" : true }
   ] );

从这个系列中,我想做一些与众不同的事。这里的区别是工作正常,但总和不起作用。

这是我用过的查询:

db.Collection.aggregate([
        //where query
        { "$match": { UserName: "USER",FacilityID:"FID",ProjectID:"ID" } },
        //distinct column 
        { "$group": { _id: { ProjectID: "$ProjectID", Task: "$Text", Phase: "$phase", Comments: "$Comments", TaskType: "$TaskType", Items: "$Items", UserName: "$UserName", IsBillable: "$IsBillable", Date: "$StartDate", Hours:{$sum:"$Hours" } }} },
        //provide column name for the output
        { "$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: [ "$_id.Hours", 60 ] } } }
    ]);

我得到的结果是这样的。它不计算值的总和。 enter image description here

此处未添加总小时数。它应该返回4但是它返回2。

任何人都可以解决我的问题吗?

1 个答案:

答案 0 :(得分:2)

$ group阶段具有以下原型形式:

// Remove items before fetching new ones to triggers the ng-if.
$scope.all_items = []; 
ServGetItemsById.servicioGetItemsById(id).success(function(data, status){
...

您的field1 小时且accumulator1为 sum , 因此,您的聚合应该是这样的:

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

给定测试数据的输出:

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 ] 
         } 
      }
    },

  ]
);

进一步阅读here