我们说我的收藏品有用户名和唯一的提交ID。
{"name" : "a","commit_id": "078d40cc537"},
{"name" : "b","commit_id": "078d40cc538"},
{"name" : "c","commit_id": "078d40cc539"},
{"name" : "a","commit_id": "078d40cc540"},
{"name" : "c","commit_id": "078d40cc541"},
{"name" : "c","commit_id": "078d40cc542"},
{"name" : "d","commit_id": "078d40cc543"}
我想生成按提交数量分组的输出。任何帮助将不胜感激。
1 commit = 2 #user b,d
2 commits = 1 #user a
3 commits = 1 #user c
答案 0 :(得分:1)
不确定。聚合“管道”正是如此,因为您可以将“链”或“管道”阶段组合在一起。要获得结果,您基本上需要连续两个$group
阶段:
db.commits.aggregate([
{ "$group": {
"_id": "$name",
"commits": { "$sum": 1 }
}},
{ "$group": {
"_id": "$commits",
"users": { "$push": "$_id" },
"howMany": { "$sum": 1 }
}},
{ "$sort": { "_id": -1 } }
])
所以每个“用户”的第一个总数,第二个按“计数”收集它们。可选择按以下形式排序:
{ "_id" : 3, "users" : [ "c" ], "howMany": 1 }
{ "_id" : 2, "users" : [ "a" ], "howMany": 1 }
{ "_id" : 1, "users" : [ "d", "b" ], "howMany": 2 }
对舞台出现的次数没有限制(在BSON大小限制内),因此您不仅限于拥有单个$ group或其他管道阶段。