来自文档和子文档的 $sum 按“$author”分组 (MongoDB)

2024-04-27

这是我的收藏:

{
        "_id" : 10926400,
        "votes": 131,
        "author": "Jesse",
        "comments" : [
                {
                        "id" : 1,
                        "votes": 31,
                        "author": "Mirek"
                },
                {
                        "id": 2,
                        "votes": 13,
                        "author": "Leszke"
                }
        ]
},
{
        "_id" : 10926401,
        "votes": 75,
        "author": "Mirek",
        "comments" : [
                {
                        "id" : 1,
                        "votes": 17,
                        "author": "Jesse"
                },
                {
                        "id": 2,
                        "votes": 29,
                        "author": "Mirek"
                }
        ]
}

而且我要$sum的值votes and comments.votes每个author

预期输出(sort $votes: -1):

"Mirek" total votes: 31 + 75 + 29 = 135

"Jesse" total votes: 131 + 17 = 148

"Leszke total votes: 13

不是立即可见,但有可能。您在这里需要做的是将顶级文档与注释数组结合起来,而不是重复它。这是一种首先将内容作为两个数组连接成一个单一数组的方法,然后$unwind http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/对内容进行分组:

db.collection.aggregate([
    { "$group": {
        "_id": "$_id",
        "author": { 
            "$addToSet": {
                "id": "$_id",
                "author": "$author",
                "votes": "$votes"
            }
        },
        "comments": { "$first": "$comments" }
    }},
    { "$project": {
        "combined": { "$setUnion": [ "$author", "$comments" ] }
    }},
    { "$unwind": "$combined" },
    { "$group": {
        "_id": "$combined.author",
        "votes": { "$sum": "$combined.votes" }
    }},
    { "$sort": { "votes": -1 } }
])

给出输出:

{ "_id" : "Jesse", "votes" : 148 }
{ "_id" : "Mirek", "votes" : 135 }
{ "_id" : "Leszke", "votes" : 13 }

即使跳过第一个$group http://docs.mongodb.org/manual/reference/operator/aggregation/group/阶段并以不同的方式制作组合数组:

db.collection.aggregate([
    { "$project": {
        "combined": { 
            "$setUnion": [
                { "$map": {
                    "input": { "$literal": ["A"] },
                    "as": "el",
                    "in": { 
                        "author": "$author",
                        "votes": "$votes"
                    }
                }},
                "$comments"
            ] 
        }
    }},
    { "$unwind": "$combined" },
    { "$group": {
        "_id": "$combined.author",
        "votes": { "$sum": "$combined.votes" }
    }},
    { "$sort": { "votes": -1 } }
])

那些使用诸如$setUnion http://docs.mongodb.org/manual/reference/operator/aggregation/setUnion/乃至$map http://docs.mongodb.org/manual/reference/operator/aggregation/map/从 MongoDB 2.6 开始引入。这使得它更简单,但仍然可以在缺乏这些运算符的早期版本中完成,遵循大致相同的原则:

db.collection.aggregate([
    { "$project": {
        "author": 1,
        "votes": 1,
        "comments": 1,
        "type": { "$const": ["A","B"] }
    }},
    { "$unwind": "$type" },
    { "$unwind": "$comments" },
    { "$group": { 
        "_id": {
          "$cond": [
              { "$eq": [ "$type", "A" ] },
              { 
                  "id": "$_id", 
                  "author": "$author",
                  "votes": "$votes"
              },
              "$comments"
          ]
        }
    }},
    { "$group": {
        "_id": "$_id.author",
        "votes": { "$sum": "$_id.votes" }
    }},
    { "$sort": { "votes": -1 } }
])

The $const未记录,但存在于存在聚合框架的所有 MongoDB 版本中(从 2.2 开始)。 MongoDB 2.6 推出$literal http://docs.mongodb.org/manual/reference/operator/aggregation/literal/它本质上链接到相同的底层代码。它在这里被用于两种情况,要么为数组提供模板元素,要么引入要展开的数组,以便在两个操作之间提供“二元选择”。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

来自文档和子文档的 $sum 按“$author”分组 (MongoDB) 的相关文章

随机推荐