MongoDB SELECT COUNT GROUP BY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23116330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB SELECT COUNT GROUP BY
提问by Steven
I am playing around with MongoDB trying to figure out how to do a simple
我在玩 MongoDB 试图弄清楚如何做一个简单的
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I can do it using some really weird group syntax
但我似乎无法使用聚合函数弄清楚。我可以使用一些非常奇怪的组语法来做到这一点
db.user.group({
"key": {
"province": true
},
"initial": {
"count": 0
},
"reduce": function(obj, prev) {
if (true != null) if (true instanceof Array) prev.count += true.length;
else prev.count++;
}
});
But is there an easier/faster way using the aggregate function?
但是有没有使用聚合函数更简单/更快的方法?
回答by Anand Jayabalan
回答by csharpbd
I need some extra operation based on the result of aggregate function. Finally I've found some solution for aggregate function and the operation based on the result in MongoDB. I've a collection Request
with field request, source, status, requestDate
.
我需要一些基于聚合函数结果的额外操作。最后我根据MongoDB中的结果找到了一些聚合函数和操作的解决方案。我有一个Request
带有 field的集合request, source, status, requestDate
。
Single Field Group By & Count:
单字段分组依据和计数:
db.Request.aggregate([
{"$group" : {_id:"$source", count:{$sum:1}}}
])
Multiple Fields Group By & Count:
多个字段分组和计数:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}}
])
Multiple Fields Group By & Count with Sort using Field:
多个字段分组和计数使用字段进行排序:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"_id.source":1}}
])
Multiple Fields Group By & Count with Sort using Count:
多个字段分组和计数使用计数进行排序:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"count":-1}}
])
回答by KitkatJohn
If you need multiple columns to group by, follow this model. Here I am conducting a count by status
and type
:
如果您需要多个列作为分组依据,请遵循此模型。在这里,我正在通过status
和进行计数type
:
db.BusinessProcess.aggregate({
"$group": {
_id: {
status: "$status",
type: "$type"
},
count: {
$sum: 1
}
}
})
回答by andre
Additionally if you need to restrict the grouping you can use:
此外,如果您需要限制分组,您可以使用:
db.events.aggregate(
{$match: {province: "ON"}},
{$group: {_id: "$date", number: {$sum: 1}}}
)
回答by MattM
Starting in MongoDB 3.4, you can use the $sortByCount
aggregation.
从 MongoDB 3.4 开始,您可以使用$sortByCount
聚合。
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
根据指定表达式的值对传入文档进行分组,然后计算每个不同组中的文档数。
https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
For example:
例如:
db.contest.aggregate([
{ $sortByCount: "$province" }
]);
回答by prule
This type of query worked for me:
这种类型的查询对我有用:
db.events.aggregate({$group: {_id : "$date", number: { $sum : 1} }} )
See http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/
请参阅http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/
回答by Riha
Mongo shell command that worked for me:
对我有用的 Mongo shell 命令:
db.getCollection(<collection_name>).aggregate([{"$match": {'<key>': '<value to match>'}}, {"$group": {'_id': {'<group_by_attribute>': "$group_by_attribute"}}}])