gpt4 book ai didi

mongodb - 聚合/分组 w/a Sum

转载 作者:可可西里 更新时间:2023-11-01 10:01:01 24 4
gpt4 key购买 nike

我正在努力了解 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 - 聚合/分组 w/a Sum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com