MongoDB按键值对进行聚合/分组

我的数据看起来像这样:

    { 
            "_id" : "9aa072e4-b706-47e6-9607-1a39e904a05a", 
            "customerId" : "2164289-4", 
            "channelStatuses" : {
                    "FOO" : {
                    "status" : "done"
                    }, 
                    "BAR" : {
                    "status" : "error"
                    }
            }, 
            "channel" : "BAR", 
    }

我的聚合/组看起来像这样:

    { 
            "_id" : {
                    "customerId" : "$customerId", 
                    "channel" : "$channel", 
                    "status" : "$channelStatuses[$channel].status"
            }, 
                    "count" : {
                    "$sum" : 1
            }
    }

所以基本上通过示例数据,该组应该给我一个分组的组:

   {"customerId": "2164289-4", "channel": "BAR", "status": "error"}

但是我不能在聚合/组中使用[] -indexing.我该怎么做呢?

最佳答案 使用.aggregate()无法获得当前结构所需的结果.您“可以”更改结构以使用数组而不是命名键,操作实际上非常简单.

所以有一个文件,如:

    { 
            "_id" : "9aa072e4-b706-47e6-9607-1a39e904a05a", 
            "customerId" : "2164289-4", 
            "channelStatuses" : [
                {
                    "channel": "FOO",
                    "status" : "done"
                }, 
                {
                    "channel": "BAR",
                    "status" : "error"
                }
            ], 
            "channel" : "BAR", 
    }

然后,您可以使用$filter,$map$arrayElemAt在现代版本中执行此操作:

    { "$group": {
        "_id": {
            "customerId" : "$customerId", 
            "channel" : "$channel", 
            "status": {
                "$arrayElemAt": [
                    { "$map": {
                        "input": { "$filter": {
                            "input": "$chanelStatuses",
                            "as": "el", 
                            "cond": { "$eq": [ "$$el.channel", "$channel" ] }
                        }},
                        "as": "el",
                        "in": "$$el.status"
                    }},
                    0
                ]
            }
        },
        "count": { "$sum": 1 }
    }}

较早版本的MongoDB将需要$unwind来访问匹配的数组元素.

在MongoDB 2.6中,您仍然可以在展开之前“预过滤”数组:

[
    { "$project": {
        "customerId": 1,
        "channel": 1,
        "status": {
            "$setDifference": [
                { "$map": {
                    "input": "$channelStatuses",
                    "as": "el",
                    "in": {
                        "$cond": [
                            { "$eq": [ "$$el.channel", "$channel" ] },
                            "$$el.status",
                            false
                        ]
                    }
                }},
                [false]
            ]
        }
    }},
    { "$unwind": "$status" },
    { "$group": {
        "_id": {
            "customerId": "$customerId",
            "channel": "$channel",
            "status": "$status"
        },
        "count": { "$sum": 1 }
    }}
]

之前的任何事情你都会在$unwind之后“过滤”:

[
    { "$unwind": "$channelStatuses" },
    { "$project": {
        "customerId": 1,
        "channel": 1,
        "status": "$channelStatuses.status",
        "same": { "$eq": [ "$channelStatuses.status", "$channel" ] }
    }},
    { "$match": { "same": true } },
    { "$group": {
        "_id": "$_id",
        "customerId": { "$first": "$customerId" },
        "channel": { "$first": "$channel" },
        "status": { "$first": "$status" }
    }},
    { "$group": {
        "_id": {
            "customerId": "$customerId",
            "channel": "$channel",
            "status": "$status"
        },
        "count": { "$sum": 1 }
    }}
]

在比MongoDB 2.6更小的版本中,您还需要$project两个字段之间的相等性测试结果,然后在单独的阶段中对结果进行$匹配.您可能还会注意到“两个”$group阶段,因为第一个阶段会在过滤器之后通过$first累加器删除任何可能的“通道”值的重复项.以下$group与上一个列表中的完全相同.

但是如果你不能改变结构并且需要“灵活”匹配你不能提供每个名字的键,那么你必须使用mapReduce:

db.collection.mapReduce(
    function() {
       emit({
           "customerId": this.customerId,
           "channel": this.channel,
           "status": this.channelStatuses[this.channel].status
       },1);
    },
    function(key,values) {
        return Array.sum(values);
    },
    { "out": { "inline": 1 } }
)

你当然可以使用那种表示法

点赞