Python MongoDB聚合然后平均
我有一个使用聚合框架的MongoDB查询,如下所示:Python MongoDB聚合然后平均,python,mongodb,aggregation-framework,Python,Mongodb,Aggregation Framework,我有一个使用聚合框架的MongoDB查询,如下所示: System._get_collection().aggregate([ { "$match": { "system_id": system.id, "utc_timestamp": { "$gte": datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0
System._get_collection().aggregate([
{ "$match": {
"system_id": system.id,
"utc_timestamp": {
"$gte": datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=1),
"$lt": datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
}
}},
{ "$group": {
"_id": { "$dayOfYear": "$utc_timestamp" },
"correct": {
"$sum": { "$cond": [
{ "$eq": [ "$status", 455 ]}, 1, 0
]}
},
"total_count": { "$sum": 1 }
}},
{ "$project": {
"correctness": {
"$cond": [
{ "$eq": [ "$correct", 0 ] },
0,
{ "$multiply" : [{"$divide": [ "$correct", "$total_count" ]}, 100] }
]
}
}}
])
{'result': [{'_id': 272, 'correctness': 99.89373007438896}], 'ok': 1.0}
{'result': [{'_id': 272, 'correctness': 99.89373007438896, 'delay': 5}], 'ok': 1.0}
输出如下所示:
System._get_collection().aggregate([
{ "$match": {
"system_id": system.id,
"utc_timestamp": {
"$gte": datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=1),
"$lt": datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
}
}},
{ "$group": {
"_id": { "$dayOfYear": "$utc_timestamp" },
"correct": {
"$sum": { "$cond": [
{ "$eq": [ "$status", 455 ]}, 1, 0
]}
},
"total_count": { "$sum": 1 }
}},
{ "$project": {
"correctness": {
"$cond": [
{ "$eq": [ "$correct", 0 ] },
0,
{ "$multiply" : [{"$divide": [ "$correct", "$total_count" ]}, 100] }
]
}
}}
])
{'result': [{'_id': 272, 'correctness': 99.89373007438896}], 'ok': 1.0}
{'result': [{'_id': 272, 'correctness': 99.89373007438896, 'delay': 5}], 'ok': 1.0}
我想在结果中添加一个字段,它是每个匹配的System
文档中所有delay
字段的平均值
我试图将“delay”:{“$avg”:“$delay”},
添加到$group
部分,但没有改变结果
我想要这样的结果:
System._get_collection().aggregate([
{ "$match": {
"system_id": system.id,
"utc_timestamp": {
"$gte": datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=1),
"$lt": datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
}
}},
{ "$group": {
"_id": { "$dayOfYear": "$utc_timestamp" },
"correct": {
"$sum": { "$cond": [
{ "$eq": [ "$status", 455 ]}, 1, 0
]}
},
"total_count": { "$sum": 1 }
}},
{ "$project": {
"correctness": {
"$cond": [
{ "$eq": [ "$correct", 0 ] },
0,
{ "$multiply" : [{"$divide": [ "$correct", "$total_count" ]}, 100] }
]
}
}}
])
{'result': [{'_id': 272, 'correctness': 99.89373007438896}], 'ok': 1.0}
{'result': [{'_id': 272, 'correctness': 99.89373007438896, 'delay': 5}], 'ok': 1.0}
注意“延迟”:5
以上应为计算的平均值
我是否需要以某种方式将其添加到投影中?或者,我做错了什么吗?除了
\u id
之外,您从$project
中省略的任何字段都不包括在输出中,因此您只需向其添加延迟
:
{ "$project": {
"correctness": {
"$cond": [
{ "$eq": [ "$correct", 0 ] },
0,
{ "$multiply" : [{"$divide": [ "$correct", "$total_count" ]}, 100] }
]
},
"delay": 1
}}