Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MongoDB聚合然后平均_Python_Mongodb_Aggregation Framework - Fatal编程技术网

Python 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

我有一个使用聚合框架的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) - 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
}}