mongodb - 带总和的聚合/分组

标签 mongodb

我正在尝试了解 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 },

谢谢

最佳答案

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

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

关于mongodb - 带总和的聚合/分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715284/

相关文章:

mongodb - 日期范围在聚合管道中不起作用,但在 find() 中起作用

javascript - Meteor 服务器在请求缓慢时自行重启

javascript - Mongoose - 在更新中删除数组元素

node.js - MongoDb findone() 结果返回 null

javascript - 使用 $and 和 $in 运算符过滤 mongodb 中标签组的项目时出现问题

java - MongoDB : Converting String Field Into Int using JAVA

javascript - 复合索引覆盖以前的文档

mongodb - 在 MongoDB 中通过 id 更新文档中的字段

node.js - 在默认模式下使用自定义函数?

node.js - 我如何使用nodejs在一段时间后动态更新数据?