Mongo DB聚合不起作用

Hap*_*der 2 mongodb aggregation-framework

我在mongo集合(datacollection)中有以下数据:

{
  "_id": ObjectId("54980efef7db7d9b018d375b"),
  "sub_id": "1234567890",
  "points": "10",
  "oid": "1",
  "name": "test",
  "app": "e53abc6d12fea63c80e4c2e1d49e",
  "source": "mysource",
  "product_name": "browser",
  "added": ISODate("2014-12-22T12:30:54.618Z"),
  "added_human": "2014-12-22 06:30:54",
  "checked": true
}
Run Code Online (Sandbox Code Playgroud)

我实际上需要找出按日期分组的点数之和.SQL查询将如下所示:SELECT DATE(已添加)为point_date,SUM(points)AS total_points FROM mytable WHERE在'from'AND'与'GROUP BY DATE(已添加)之间添加.

所以我使用以下聚合:

db.datacollection.aggregate(
    { 
        $match:{
            "added" : {$gte:ISODate("2014-12-01"), $lt:ISODate("2014-12-25")},   
            "source":"mysource"  
        },   
        $group : {
            "_id" : { month: { $month: "$added" }, day: { $dayOfMonth: "$added" }, year: { $year: "$added" } },
            "total_points":{$sum:"$points"}   
        }  
    }  
);
Run Code Online (Sandbox Code Playgroud)

但这显示出来了

"errmsg" : "exception: A pipeline stage specification object must contain exactly one field.",
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题还是有更好的方法来进行这种计算?

Dis*_*ser 9

用这个:

db.datacollection.aggregate(
    { 
        $match:{
            "added" : {$gte:ISODate("2014-12-01"), $lt:ISODate("2014-12-25")},   
            "source":"mysource"  
        }
    },   
    {   $group : {
            "_id" : { month: { $month: "$added" }, day: { $dayOfMonth: "$added" }, year: { $year: "$added" } },
            "total_points":{$sum:"$points"}   
        }  
    }  
);
Run Code Online (Sandbox Code Playgroud)

你应该使用这种格式({}成对中的每个管道)

db.collection.aggrgation ( 
   { $group: ....},
   { $match : .... },
   ...
})
Run Code Online (Sandbox Code Playgroud)

但你用过:

db.collection.aggrgation ( 
   { $group: ....,
     $match : ....,
     ...
   }
})
Run Code Online (Sandbox Code Playgroud)