gpt4 book ai didi

mongodb - $sum :1 mean in Mongo 是什么意思

转载 作者:行者123 更新时间:2023-12-02 09:33:57 26 4
gpt4 key购买 nike

我有一个集合foo:

{ "_id" : ObjectId("5837199bcabfd020514c0bae"), "x" : 1 }
{ "_id" : ObjectId("583719a1cabfd020514c0baf"), "x" : 3 }
{ "_id" : ObjectId("583719a6cabfd020514c0bb0") }

我使用这个查询:

db.foo.aggregate({$group:{_id:1, avg:{$avg:"$x"}, sum:{$sum:1}}})

然后我得到一个结果:

{ "_id" : 1, "avg" : 2, "sum" : 3 }

此查询中的 {$sum:1} 意味着什么?

最佳答案

来自official docs :

When used in the $group stage, $sum has the following syntax and returns the collective sum of all the numeric values that result from applying a specified expression to each document in a group of documents that share the same group by key:

{ $sum: < expression > }

由于在您的示例中表达式为 1,因此它将为组中的每个文档聚合值 1,从而生成每个组的文档总数。

关于mongodb - $sum :1 mean in Mongo 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40791907/

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