【问题标题】:MongoDB aggregation $divide computed fieldsMongoDB 聚合 $divide 计算字段
【发布时间】:2019-04-06 02:11:01
【问题描述】:

我正在尝试根据计算字段计算 MongoDB 查询中的百分比 - 不确定这是否可能。我想做的是计算失败百分比:(failed count / total) * 100

以下是一些示例文档:

    {
            "_id" : ObjectId("52dda5afe4b0a491abb5407f"),
            "type" : "build",
            "time" : ISODate("2014-01-20T22:39:43.880Z"),
            "data" : {
                    "buildNumber" : 30,
                    "buildResult" : "SUCCESS"
            }
    },
    {
            "_id" : ObjectId("52dd9fede4b0a491abb5407a"),
            "type" : "build",
            "time" : ISODate("2014-01-20T22:15:07.901Z"),
            "data" : {
                    "buildNumber" : 4,
                    "buildResult" : "FAILURE"
            }
    },
    {
            "_id" : ObjectId("52dda153e4b0a491abb5407b"),
            "type" : "build",
            "time" : ISODate("2014-01-20T22:21:07.790Z"),
            "data" : {
                    "buildNumber" : 118,
                    "buildResult" : "SUCCESS"
            }
    }

这是我正在尝试使用的查询。问题出在 FailPercent/$divide 行中:

db.col.aggregate([    
    { $match: { "data.buildResult" : { $ne : null } } },
    { $group: {          
        _id: {              
            month: { $month: "$time" },             
            day: { $dayOfMonth: "$time" },             
            year: { $year: "$time" },                       
        },         
        Aborted: { $sum: { $cond :  [{ $eq : ["$data.buildResult", "ABORTED"]}, 1, 0]} },
        Failure: { $sum: { $cond :  [{ $eq : ["$data.buildResult", "FAILURE"]}, 1, 0]} },
        Unstable: { $sum: { $cond : [{ $eq : ["$data.buildResult", "UNSTABLE"]}, 1, 0]} },
        Success: { $sum: { $cond :  [{ $eq : ["$data.buildResult", "SUCCESS"]}, 1, 0]} },
        Total: { $sum: 1 },
        FailPercent: { $divide: [ "Failure", "Total" ] }
    } },     
    { $sort: { "_id.year": 1, "_id.month": 1, "_id.day": 1 } } 
])

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    你几乎明白了。唯一需要的更改是您必须在额外的project 阶段计算FailPercent,因为只有在group 阶段完成后才能获得总数。试试这个:

    db.foo.aggregate([    
        { $match: { "data.buildResult" : { $ne : null } } },
        { $group: {          
            _id: {              
                month: { $month: "$time" },             
                day: { $dayOfMonth: "$time" },             
                year: { $year: "$time" },                       
            },         
            Aborted: { $sum: { $cond :  [{ $eq : ["$data.buildResult", "ABORTED"]}, 1, 0]} },
            Failure: { $sum: { $cond :  [{ $eq : ["$data.buildResult", "FAILURE"]}, 1, 0]} },
            Unstable: { $sum: { $cond : [{ $eq : ["$data.buildResult", "UNSTABLE"]}, 1, 0]} },
            Success: { $sum: { $cond :  [{ $eq : ["$data.buildResult", "SUCCESS"]}, 1, 0]} },
            Total: { $sum: 1 }
        } }, 
        {$project:{Aborted:1, Failure:1, Unstable:1, Success:1, Total:1, FailPercent: { $divide: [ "$Failure", "$Total" ]}}},
        { $sort: { "_id.year": 1, "_id.month": 1, "_id.day": 1 } } 
    ])
    

    【讨论】:

    • 其他示例尝试使用多个分组,这并不适用于所有情况,也不是那么优雅。这个不错!
    • 很好的例子!
    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多