目录 · 4 个章节
Robomongo的聚合查询如下:
db.accounts.aggregate([ { $match: { "date": {"$gte" : "2014/07/01" , "$lte" : "2014/07/31"}} }, { $group: { _id: "$kind.code_no", total: { $sum: "$cash" } } }, { $sort: { total: -1 } } ])
查出数据:###
{ "result" : [ { "_id" : "income", "total" : 1000 }, { "_id" : "payout", "total" : 300 } ], "ok" : 1 }
node方法中的代码如下,Accounts是model
Accounts.aggregate([ { $match: { "date": {"$gte" : firtday , "$lte" : endday}} }, { $group: { _id: "$kind.code_no", total: { $sum: "$cash" } } }, { $sort: { total: -1 } } ],function (err, docs) { if (err) { return callback(err); } return callback(null, docs); });