Mongo聚合&不同

时间:2014-09-16 05:48:20

标签: mongodb distinct mongodb-query aggregation-framework

我们假设我的集合中包含用户名,存储库名称和唯一的提交ID。

{"name" : "a","commit_id": "078d40cc537","repo": "r1"},
{"name" : "b","commit_id": "078d40cc538","repo": "r2"},
{"name" : "c","commit_id": "078d40cc539","repo": "r3"},
{"name" : "a","commit_id": "078d40cc540","repo": "r1"},
{"name" : "c","commit_id": "078d40cc541","repo": "r1"},
{"name" : "c","commit_id": "078d40cc542","repo": "r3"},
{"name" : "d","commit_id": "078d40cc543","repo": "r1"}

我想生成一个按存储库分组的输出,其中包含#distinct users和total commit。

repo:"r1" , distinct_users:"3", total_commits:"4"
repo:"r3" , distinct_users:"1", total_commits:"2"
repo:"r2" , distinct_users:"1", total_commits:"1"

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

两个分组管道阶段。但您可能会遗漏$sum不只是添加1

db.collection.aggregate([
    { "$group": {
        "_id": { "repo": "$repo", "name": "$name" },
        "commits": { "$sum": 1 }
    }},
    { "$group": {
        "_id": "$_id.repo",
        "distinct_users": { "$sum": 1 },
        "total_commits": { "$sum": "$commits" }
    }}
])