MongoDB获取dictfield /嵌入式文档的平均值

奥科博科

我有一个像这样的MongoDB文件:

{
  ...
  score: 200,
  timing: {
    rT: 124
  },
  utc_timestamp: ISODate("2014-10-19T04:45:04.772Z")
}

我可以得到score使用此查询的平均值

System._get_collection().aggregate([
            { "$match": {
                "system_id": system.id,
                "utc_timestamp": {
                    "$gte": datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=1),
                    "$lt": datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
                }
            }},
            { "$group": {
                "_id": { "$dayOfYear": "$utc_timestamp" },
                "delay": { "$avg": "$score" },
                "up": {
                    "$sum": { "$cond": [
                        { "$eq": [ "$result", 405 ]}, 1, 0
                    ]}
                },
                "total_count": { "$sum": 1 }
            }},
            { "$project": {
                "airtime": {
                    "$cond": [
                        { "$eq": [ "$up", 0 ] },
                        0,
                        { "$multiply" : [{"$divide": [ "$up", "$total_count" ]}, 100] }
                    ]
                },
                "delay": 1
            }}
        ])

请注意以下行:

"delay": { "$avg": "$score" },

如何通过字典中rT字段获取平均值timing

这不起作用:

{ "$avg": "$timing.rT" }

著名的加金

的语法{ "$avg": "$timing.rT" }正确,应该可以正常工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章