【问题标题】:Nested aggregate pipeline for mongodbmongodb 的嵌套聚合管道
【发布时间】:2016-11-12 00:21:27
【问题描述】:

我有一个 mongodb 集合,其中包含以下格式的大量文档:

{
    "_id": {
        "$oid": "581f9eae05711c35f461d779"
    },
    "eventID": "Illinois84bc00edc91c24a95573",
    "type": "voteup",
    "update": "f3a44be56669e01cf02ed685c97451ba",
    "count": 1,
}

我需要能够从我的服务器对集合运行聚合并返回一个 JSON 对象:

按 eventID 对数据进行分组(有很多 eventID),然后按更新对这些组中的文档进行分组(有很多更新,每个更新都与一个 eventID 相关联),然后对于每个 eventID/更新组,对“计数”求和“voteup”和“votedown”类型的每个文档的值。

所以实际输出可能如下所示:

{
    "events": [
    //  array of objects, one for each unique eventID
        {
            "eventID": "Idaho4532543trt3424f",
            "updates": [
            //  array of objects, one for each unique update
                {
                    "update": "rh43782ty738tywuioty34",
                    "voteup": 12,  // the number of documents with that eventID/update which are type "voteup"
                    "votedown": 1  // the number of documents with that eventID/update which are type "voteudown"
                },
                {
                    "update": "hfu89h834hw8uoglthyw78",
                    "voteup": 2,
                    "votedown": 32
                },
                {
                    "update": "as09g8fgdsiuhgofhdosug",
                    "voteup": 123,
                    "votedown": 5
                }
            ]
        },
        //  here's another event...
        {
            "eventID": "Texas38tr943uytwo9grs",
            "updates": [
                {
                    "update": "2ty738tywuioty34rh4378",
                    "voteup": 0,
                    "votedown": 1
                },
                {
                    "update": "34hw8uoglthyw78hfu89hv",
                    "voteup": 22,
                    "votedown": 72
                },
                {
                    "update": "dsiuhgofhdosugas09g8fg",
                    "voteup": 67,
                    "votedown": 11
                }
            ]
        }
    ]
}

下面的任务是计算有多少 eventID/update/type 文档:

{
    $group: {

        _id: { eventID: "$eventName", updateID: "$update", sentiment: "$type" }

    }
},

但输出只是每个的平面数组。有没有办法让聚合产生一个像上面那样的嵌套对象?

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    首先您可以在eventIDupdate 上进行分组,然后在eventID 上进行分组,同时将$pushupdate 组合成一个数组

    {$group: {
        _id: {eventID: '$eventID', update: '$update'},
        voteup: {$sum: {$cond: [
                {$eq: ['$type', {$literal: 'voteup'}]},
                1,
                0
            ]}},
        votedown: {$sum: {$cond: [
                {$eq: ['$type', {$literal: 'voteup'}]},
                0,
                1
            ]}}
    }},
    
    {$group: {
        _id: {eventID: '$_id.eventID'},
        updates: {$push: {
            update: '$_id.update',
            voteup: '$voteup',
            votedown: '$votedown'
        }}
    }}
    

    【讨论】:

    • 里面有一个小错误;在第二个 $group 中,$id.eventID 需要是 $_id.eventID。
    • ...但是您的解决方案正是我所追求的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2021-03-13
    • 1970-01-01
    • 2017-07-26
    • 2017-07-16
    • 2019-07-20
    • 1970-01-01
    相关资源
    最近更新 更多