MongoDB - 复杂分组查询

时间:2015-09-28 10:10:58

标签: mongodb mongodb-query aggregation-framework

我有以下MongoDB聚合查询,它按IDC,类型和群集进行分组 - 效果很好。

我想在此现有分组中另外分组"环境",内部。请参阅下面的查询,我现有的输出,以及我想看到的内容(所需的输出)。

如果您有任何疑问或希望查看来源(我认为没有必要,因为它会占用问题,请发表评论)。

由于

示例来源(约1000份文件):

   {
        "_id":"55d5dc40281077b6d8af1bfa",
        "hostname":"1",
        "domain":"domain",
        "description":"VMWare ESXi 5",
        "cluster":1,
        "type":"Physical",
        "os":"EXSi",
        "idc":"AMS",
        "environment":"DR",
        "deviceclass":"host",
        "cores":64,
        "memory":256,
        "clusters":0,
        "customer":"MnS",
        "mounts":[],
        "roles":["ESX-HOST"],
        "ipset":{"backnet":"1"},
        "frontnet":[],
        "created":"2015-09-28T11:12:36.526Z"
    }

查询:

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" : "LH5"
    },
    "Physical" : {
            "SumCores" : 192,
            "SumMemory" : 768
    },
    "Virtual" : {
            "SumCores" : 112,
            "SumMemory" : 384
    }
}

我想要的输出是:

[
{
    "_id": {
        "cluster": 1,
        "idc": "LH8"
    },
    "Physical": [
        {
            "environment": "DR",
            "SumCores": 256,
            "SumMemory": 1024
        },
        {
            "environment": "PROD",
            "SumCores": 256,
            "SumMemory": 1024
        }
    ],
    "Virtual": [
        {
            "environment": "DR",
            "SumCores": 232,
            "SumMemory": 469
        },
        {
            "environment": "PROD",
            "SumCores": 232,
            "SumMemory": 469
        }
    ]
}
]

基本上,我想根据环境对总和进行分组

1 个答案:

答案 0 :(得分:1)

与初始查询(actually written by myself)非常相似,您真正需要做的就是将该字段详细信息添加到_id的初始$group中,然后将其传入后续数组条目:

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

但是你也真的"应该使用我建议你首先做的查询表单,因为很明显你想要做的就是在模板中显示它,并且循环数组内容应该非常简单:

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