2

我试图了解 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 },

谢谢

4

2 回答 2

3
db.yourCollection.aggregate(
    { $group : {
        _id : "$col1",
        fps : { $sum : "$fps" },
        lat : { $sum : "$lat" }
    }}
);

看一下这个!!!http://docs.mongodb.org/manual/reference/operator/aggregation/group/

于 2013-10-31T19:49:14.993 回答
1

我认为这应该可以解决问题:

db.collectionName.aggregate(
    { $group: { _id: "$col1", fps: { $sum: "$fps" }, lat: { $sum: "$lat" }}}
)
于 2013-10-31T19:50:10.347 回答