我试图了解Mongo的整个聚合框架。我在这里是个新手。
I have the following documents:
{ "col1": "camera", "fps": 1, "lat": 3 },
{ "col1": "camera", "fps": 3, "lat": 2 }
{ "col1": "recorders", "fps": 9, "lat": 7 }
{ "col1": "cell", "fps": 8, "lat": 1 }
{ "col1": "cell", "fps": 4, "lat": 3 }
如何设置一些内容以将结果返回为:
{ "col1": "camera", "fps": 4, "lat": 5 },
{ "col1": "recorders", "fps": 9, "lat": 7 },
{ "col1": "cell", "fps": 12, "lat": 4 },
由于
答案 0 :(得分:3)
db.yourCollection.aggregate(
{ $group : {
_id : "$col1",
fps : { $sum : "$fps" },
lat : { $sum : "$lat" }
}}
);
检查出来!!! http://docs.mongodb.org/manual/reference/operator/aggregation/group/
答案 1 :(得分:1)
我认为这应该可以解决问题:
db.collectionName.aggregate(
{ $group: { _id: "$col1", fps: { $sum: "$fps" }, lat: { $sum: "$lat" }}}
)