mongoDB问题
我的mongodb中有一个集合,其中包含以下列:
{"_id" : ObjectId("5443cddc425e215c7290a179"), "orderid" : 1002854, "customerid" : 45978, "campaignid" : 2141, "orderdate" : "2009-10-13 00:00:00", "city" : "NEWTON", "state" : "MA", "zipcode" : 2459, "paymenttype" : "VI", "totalprice" : 190, "numorderlines" : 3, "numunits" : 3}
有239个不同的campaignid,它们的值都应为1,但它们都有0。
现在我希望每个campaignid分组的numunits总数,我使用以下语句:
db.orders.aggregate({$group: {_id: "$campaignid", Numunits: {$sum:" $numunits"}}})
它给出了以下结果:
{ "_id" : 2146, "Numunits" : 0} , { "_id" : 2111, "Numunits" : 0} etc...
现在预期的结果是:
{ "_id": 2146, "Numunits" : 93} , { "_id" : 2111, "Numunits" : 23} etc..
答案 0 :(得分:0)
如果有的话,Mongo不会为你修剪空间。试试这个:
db.orders.aggregate(
[
{$group: {
_id: "$campaignid",
Numunits: {$sum:"$numunits"}
}
}
])