在 MongoDB 中按条件分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24153476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Group By Condition in MongoDB
提问by okoboko
I have a series of documents (check events) in MongoDB that look like this:
我在 MongoDB 中有一系列文件(检查事件),如下所示:
{
"_id" : ObjectId("5397a78ab87523acb46f56"),
"inspector_id" : ObjectId("5397997a02b8751dc5a5e8b1"),
"status" : 'defect',
"utc_timestamp" : ISODate("2014-06-11T00:49:14.109Z")
}
{
"_id" : ObjectId("5397a78ab87523acb46f57"),
"inspector_id" : ObjectId("5397997a02b8751dc5a5e8b2"),
"status" : 'ok',
"utc_timestamp" : ISODate("2014-06-11T00:49:14.109Z")
}
I need to get a result set that looks like this:
我需要得到一个如下所示的结果集:
[
{
"date" : "2014-06-11",
"defect_rate" : '.92'
},
{
"date" : "2014-06-11",
"defect_rate" : '.84'
},
]
In other words, I need to get the average defect rate per day. Is this possible?
换句话说,我需要获得每天的平均缺陷率。这可能吗?
回答by Neil Lunn
The aggregation framework is what you want:
聚合框架就是你想要的:
db.collection.aggregate([
{ "$group": {
"_id": {
"year": { "$year": "$utc_timestamp" },
"month": { "$month": "$utc_timestamp" },
"day": { "$dayOfMonth": "$utc_timestamp" },
},
"defects": {
"$sum": { "$cond": [
{ "$eq": [ "$status", "defect" ] },
1,
0
]}
},
"totalCount": { "$sum": 1 }
}},
{ "$project": {
"defect_rate": {
"$cond": [
{ "$eq": [ "$defects", 0 ] },
0,
{ "$divide": [ "$defects", "$totalCount" ] }
]
}
}}
])
So first you group on the day using the date aggregation operatorsand get the totalCount of items on the given day. The use of the $cond
operator here determines whether the "status" is actually a defect or not and the result is a conditional $sum
where only the "defect" values are counted.
因此,首先您使用日期聚合运算符对当天进行分组,并获取给定日期的项目总数。$cond
此处使用运算符确定“状态”是否实际上是缺陷,结果是$sum
仅计算“缺陷”值的条件。
Once those are grouped per day you simply $divide
the result, with another check with $cond
to make sure you are not dividing by zero.