代码之家  ›  专栏  ›  技术社区  ›  Matheus Barem

Mongo$group划分两个字段并显示结果[duplicate]

  •  3
  • Matheus Barem  · 技术社区  · 6 年前

    我试图根据计算字段计算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 } } 
    ])
    
    0 回复  |  直到 10 年前
        1
  •  27
  •   Anand Jayabalan    10 年前

    你差点就搞定了。唯一需要改变的是,您必须计算 FailPercent project 阶段,因为总数只有在完成后才可用 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 } } 
    ])