MongoDB - 聚合多行

时间:2015-09-17 10:33:36

标签: mongodb mongodb-query aggregation-framework

假设以下聚合查询:

Machine.aggregate(  [ { $match : {  $and: [  {"idc": req.query.idc }, {"customer":req.query.customer} ] } } ,{"$group":{_id: {"cluster":"$cluster","idc":"$idc","type":"$type"},"SumCores":{"$sum":"$cores"},"SumMemory": { "$sum":"$memory" }}}, { $sort : { idc : -1, cluster: 1 } } ]);

返回:

[
{
    "_id": {
        "cluster": 1,
        "idc": "LH5",
        "type": "Virtual"
    },
    "SumCores": 112,
    "SumMemory": 384
},
{
    "_id": {
        "cluster": 1,
        "idc": "LH5",
        "type": "Physical"
    },
    "SumCores": 192,
    "SumMemory": 768
},
{
    "_id": {
        "cluster": 1,
        "idc": "LH8",
        "type": "Virtual"
    },
    "SumCores": 232,
    "SumMemory": 469
},
{
    "_id": {
        "cluster": 1,
        "idc": "LH8",
        "type": "Physical"
    },
    "SumCores": 256,
    "SumMemory": 1024
}
]

有没有办法更改聚合以检索此所需的输出:

[
   {
    "_id": {
        "cluster": 1,
        "idc": "LH5"
    },
    "Virtual": {
        "SumCores": 112,
        "SumMemory": 384
    },
    "Physical": {
        "SumCores": 192,
        "SumMemory": 768
    }
},
{
    "_id": {
        "cluster": 1,
        "idc": "LH8"
    },
    "Virtual": {
        "SumCores": 232,
        "SumMemory": 469
    },
    "Physical": {
        "idc": "LH8",
        "type": "Physical"
    }
}
]

假设:

  • 每个IDC /群集总会有一个物理和虚拟“对”

我很高兴收到以下解决方案:

a)更改聚合查询 b)接收现有数据并通过库和/或算法将其更改为此格式

1 个答案:

答案 0 :(得分:1)

您已经在查询中执行了所有正确的操作,因为您需要$group以获得正确的金额。唯一剩下的就是把它们放在一起。

我个人会坚持使用"对#34;在数组中作为最终输出:

Machine.aggregate([ 
    { "$match": { 
        "idc": req.query.idc, "customer": req.query.customer}
    } ,
    { "$group": { 
        "_id": {
            "cluster": "$cluster",
            "idc":"$idc",
            "type": "$type"
        },
        "SumCores": { "$sum":"$cores" },
        "SumMemory": { "$sum":"$memory" }
    }},
    { "$group": {
        "_id": {
            "cluster": "$_id.cluster",
            "idc": "$_id.idc"
        },
        "data": {
            "$push": {
                "type": "$_id.type",
                "SumCores": "$SumCores",
                "SumMemory": "$SumMemory"
            }
        }
    }},
    { "$sort" : { "_id.idc": -1, "_id.cluster": 1 } }
]);

哪会给你:

{
        "_id" : {
                "cluster" : 1,
                "idc" : "LH8"
        },
        "data" : [
                {
                        "type" : "Virtual",
                        "SumCores" : 232,
                        "SumMemory" : 469
                },
                {
                        "type" : "Physical",
                        "SumCores" : 256,
                        "SumMemory" : 1024
                }
        ]
}
{
        "_id" : {
                "cluster" : 1,
                "idc" : "LH5"
        },
        "data" : [
                {
                        "type" : "Virtual",
                        "SumCores" : 112,
                        "SumMemory" : 384
                },
                {
                        "type" : "Physical",
                        "SumCores" : 192,
                        "SumMemory" : 768
                }
        ]
}

但如果你真的必须,那么你可以从数组中过滤掉匹配的元素并将它们放在自己的属性中:

Machine.aggregate([ 
    { "$match": { 
        "idc": req.query.idc, "customer": req.query.customer}
    } ,
    { "$group": { 
        "_id": {
            "cluster": "$cluster",
            "idc":"$idc",
            "type": "$type"
        },
        "SumCores": { "$sum":"$cores" },
        "SumMemory": { "$sum":"$memory" }
    }},
    { "$group": {
        "_id": {
            "cluster": "$_id.cluster",
            "idc": "$_id.idc"
        },
        "data": {
            "$push": {
                "type": "$_id.type",
                "SumCores": "$SumCores",
                "SumMemory": "$SumMemory"
            }
        }
    }},
    { "$project": {
        "Physical": {
            "$setDifference": [
                { "$map": {
                    "input": "$data",
                    "as": "el",
                    "in": {
                        "$cond": [
                            { "$eq": [ "$$el.type", "Physical" ] },
                            {
                                "SumCores": "$$el.SumCores",
                                "SumMemory": "$$el.SumMemory"
                            },
                            false
                        ]
                    }
                }},
                [false]
            ]
        },
        "Virtual": {
            "$setDifference": [
                { "$map": {
                    "input": "$data",
                    "as": "el",
                    "in": {
                        "$cond": [
                            { "$eq": [ "$$el.type", "Virtual" ] },
                            {
                                "SumCores": "$$el.SumCores",
                                "SumMemory": "$$el.SumMemory"
                            },
                            false
                        ]
                    }
                }},
                [false]
            ]
        }
    }},
    { "$unwind": "$Physical" },
    { "$unwind": "$Virtual"},
    { "$sort" : { "_id.idc": -1, "_id.cluster": 1 } }
]);

哪能为您提供结果:

{
        "_id" : {
                "cluster" : 1,
                "idc" : "LH8"
        },
        "Physical" : {
                "SumCores" : 256,
                "SumMemory" : 1024
        },
        "Virtual" : {
                "SumCores" : 232,
                "SumMemory" : 469
        }
}
{
        "_id" : {
                "cluster" : 1,
                "idc" : "LH5"
        },
        "Physical" : {
                "SumCores" : 192,
                "SumMemory" : 768
        },
        "Virtual" : {
                "SumCores" : 112,
                "SumMemory" : 384
        }
}

但第一种方法是在不需要额外通过结果的情况下为您提供相同的基本数据。

无论如何,如果你真的必须拥有这种数据格式,它真的只需要一个$group将它们组合在一起,然后是可选的阶段。但我会亲自处理任何对"对#34;在需要处理它的代码中。