MongoDB聚合基于值计数

时间:2014-03-03 14:21:18

标签: mongodb

我想知道一个MongoDB查询。我想按日期和另外一个字段(投资组合)对数据进行分组,并获取该分组中每个buildResult的计数。

示例数据如下所示:

    {
            "_id" : ObjectId("52dea764e4b0a491abb54102"),
            "type" : "build",
            "time" : ISODate("2014-01-21T16:59:16.642Z"),
            "data" : {
                    "buildNumber" : 35,
                    "buildDuration" : 1034300,
                    "portfolio" : "Shared",
                    "buildResult" : "FAILURE"
            }
    }
    {
            "_id" : ObjectId("52dea7b7e4b0a491abb54103"),
            "type" : "build",
            "time" : ISODate("2014-01-21T17:00:39.617Z"),
            "data" : {
                    "buildNumber" : 13,
                    "buildDuration" : 1186028,
                    "portfolio" : "Sample",
                    "buildResult" : "SUCCESS"
            }
    }

我正在拍摄的输出将是这样的:

    {
            "result" : [
                    {
                            "_id" : {
                                    "month" : 2,
                                    "day" : 28,
                                    "year" : 2014,
                                    "portfolio" : "Shared"
                            },
                            "aborted" : 3,
                            "failure" : 0,
                            "unstable" : 0,
                            "success" : 34
                    },
                    {
                            "_id" : {
                                    "month" : 2,
                                    "day" : 28,
                                    "year" : 2014,
                                    "portfolio" : "Sample"
                            },
                            "aborted" : 3,
                            "failure" : 2,
                            "unstable" : 0,
                            "success" : 37
                    }
            ],
            "ok" : 1
    }

我目前的查询是:

db.builds.aggregate([    
    { $match: { "data.buildResult" : { $ne : null} }},
    { $group: {          
        _id: {              
            month: { $month: "$time" },             
            day: { $dayOfMonth: "$time" },             
            year: { $year: "$time" },   
            portfolio: "$data.portfolio",                    
        },         
        aborted: { $sum: { "$data.buildResult": "ABORTED" } },
        failure: { $sum: { "$data.buildResult": "FAILURE" } },
        unstable: { $sum: { "$data.buildResult": "UNSTABLE" } },
        success: { $sum: { "$data.buildResult": "SUCCESS" } }
    } },     
    { $sort: { "_id.day": 1, "_id.month": 1, "_id.year": 1 } } 
])

我尝试过以下几行,包括$ match,$ in和其他运算符。任何帮助都将非常感激。

aborted: { $sum: { "$data.buildResult": "ABORTED" } },
failure: { $sum: { "$data.buildResult": "FAILURE" } },
unstable: { $sum: { "$data.buildResult": "UNSTABLE" } },
success: { $sum: { "$data.buildResult": "SUCCESS" } }

1 个答案:

答案 0 :(得分:2)

要实现这一点,您可以使用$cond$eq运算符,如下所示:

aborted: {$sum: {$cond : [{$eq : ["$data.buildResult", "ABORTED"]}, 1, 0]}}

修改 正如评论中Neil Lunn所指出的那样,$ cond在这里是无关紧要的,因为$ eq运算符已经返回0或1。

aborted: {$sum: {$eq : ["$data.buildResult", "ABORTED"]}}