如何使用Mongo聚合功能来计算嵌套组的总计和子总计百分比?

时间:2016-09-21 14:42:38

标签: mongodb mongodb-query aggregation-framework mongodb-aggregation

我有像这样的mongo集合中的一些文档。

 {
    "_id" : ObjectId("57e290087139be15d59408c1"),
    "groupName" : "Registration",
    "testCases" : [ 
        {
            "name" : "R1",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "R1.1",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "R2",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "R3",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "R4",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "R1",
            "browser" : "Firefox",
            "status" : "passed"
        }, 
        {
            "name" : "R2",
            "browser" : "Firefox",
            "status" : "passed"
        }, 
        {
            "name" : "R3",
            "browser" : "Firefox",
            "status" : "passed"
        }, 
        {
            "name" : "R4",
            "browser" : "Firefox",
            "status" : "failed"
        }
    ]
}

    {
    "_id" : ObjectId("57e2903b7139be15d59408c2"),
    "groupName" : "Checkout",
    "testCases" : [ 
        {
            "name" : "C1",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "C2",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "C3",
            "browser" : "Chrome",
            "status" : "failed"
        }, 
        {
            "name" : "C4",
            "browser" : "Chrome",
            "status" : "passed"
        }, 
        {
            "name" : "C1",
            "browser" : "Firefox",
            "status" : "passed"
        }, 
        {
            "name" : "C2",
            "browser" : "Firefox",
            "status" : "passed"
        }, 
        {
            "name" : "C3",
            "browser" : "Firefox",
            "status" : "passed"
        }, 
        {
            "name" : "C4",
            "browser" : "Firefox",
            "status" : "failed"
        }
    ]
}

如何使用Mongo的聚合功能来计算嵌套组的总计和小计百分比?

我期待输出结果为

预期输出示例

results: [    
    {
        "groupName": "Registration",
        "totalTests": 17,
        "section": [
            { "name": "R1", "totalTests": 17, "sectionCount": 2 }
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

您可以尝试运行以下聚合操作:

db.collection.aggregate([
    { "$unwind": "$testCases" },
    {
        "$group": {
            "_id": null,
            "testsCount": { "$sum": 1 },
            "docs": { "$push": "$$ROOT" }
        }
    },
    { "$unwind": "$docs" },
    {
        "$group": {
            "_id": {
                "groupName": "$docs.groupName",
                "testName": "$docs.testCases.name"
            },
            "count": { "$sum": 1 },
            "testsCount": { "$first": "$testsCount" }
        }
    },    
    {
        "$group": {
            "_id": "$_id.groupName",
            "totalTests": { "$first": "$testsCount" },
            "section": {
                "$push": {
                    "name": "$_id.testName", 
                    "totalTests": "$testsCount", 
                    "sectionCount": "$count"
                }
            }
        }
    }
])