MongoDB-一次汇总多个变量的最大/最小/平均值 [英] MongoDB - Aggregate max/min/average for multiple variables at once

查看:106
本文介绍了MongoDB-一次汇总多个变量的最大/最小/平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以以下格式将数据记录到MongoDB中:

I am logging data into MongoDB in the following format:

{ "_id" : ObjectId("54f2393f80b72b00079d1a53"), "outT" : 10.88, "inT3" : 22.3, "light" : 336, "humidity" : 41.4, "pressure" : 990.31, "inT1" : 22.81, "logtime" : ISODate("2015-02-28T21:55:11.838Z"), "inT2" : 21.5 }
{ "_id" : ObjectId("54f2394580b72b00079d1a54"), "outT" : 10.88, "inT3" : 22.3, "light" : 338, "humidity" : 41.4, "pressure" : 990.43, "inT1" : 22.75, "logtime" : ISODate("2015-02-28T21:55:17.690Z"), "inT2" : 311.72 }
...

如您所见,其中有一个时间元素并记录了多个读数。我想汇总所有读数,以提供按一天中的小时分组的每个变量的最大最小值和平均值。我已经设法使用以下聚合脚本对单个变量执行此操作:

As you can see there is a single time element and multiple readings logged. I want to aggregate across all of the readings to provide a max min and average for each variable grouped by hour of day. I have managed to do this for a single variable using the following aggregation script:

db.logs.aggregate(
    [ 
        { 
            $match: { 
                logtime: { 
                    $gte: ISODate("2015-03-01T00:00:00.000Z"), 
                    $lt: ISODate("2015-03-03T00:00:00.000Z") 
                }
            }
        }, 
        { 
            $project: {_id: 0, logtime: 1, outT: 1}
        }, 
        {
            $group: {
                _id: {
                    day: {$dayOfYear: "$logtime"},
                    hour: {$hour: "$logtime"}
                }, 
                average: {$avg: "$outT"}, 
                max: {$max: "$outT"}, 
                min:{$min: "$outT"} 
            }
        }
    ]
)

会产生

{ "_id" : { "day" : 61, "hour" : 22 }, "average" : 3.1878750000000116, "max" : 3.44, "min" : 3 }
{ "_id" : { "day" : 61, "hour" : 14 }, "average" : 13.979541666666638, "max" : 17.81, "min" : 8.81 }
...

我想产生类似以下的输出:

I would like to produce output which looks like:

{"outT": { output from working aggregation above },
 "inT1": { ... },
 ...
}

我尝试的所有操作似乎都在mongo控制台中引发了错误。有人可以帮忙吗?

Everything I try seems to throw an error in the mongo console. Can anyone help?

谢谢

推荐答案

包括您的 $ group 中的每个统计信息使用不同的名称,然后在 $ project 阶段之后将其重塑为您所需的格式:

You can do this by including each statistic in your $group with a different name and then following that with a $project stage to reshape it into your desired format:

db.logs.aggregate([
    { 
        $match: { 
            logtime: { 
                $gte: ISODate("2015-02-28T00:00:00.000Z"), 
                $lt: ISODate("2015-03-03T00:00:00.000Z") 
            }
        }
    }, 
    { 
        $project: {_id: 0, logtime: 1, outT: 1, inT1: 1}
    }, 
    {
        $group: {
            _id: {
                day: {$dayOfYear: "$logtime"},
                hour: {$hour: "$logtime"}
            }, 
            outT_average: {$avg: "$outT"}, 
            outT_max: {$max: "$outT"}, 
            outT_min:{$min: "$outT"},
            inT1_average: {$avg: "$inT1"}, 
            inT1_max: {$max: "$inT1"}, 
            inT1_min:{$min: "$inT1"}
        }
    },
    {
        $project: {
            outT: {
                average: '$outT_average',
                max: '$outT_max',
                min: '$outT_min'
            },
            inT1: {
                average: '$inT1_average',
                max: '$inT1_max',
                min: '$inT1_min'
            }
        }
    }        
])

这将为您提供如下输出:

This gives you output that looks like:

{
    "_id" : {
        "day" : 59,
        "hour" : 21
    },
    "outT" : {
        "average" : 10.88,
        "max" : 10.88,
        "min" : 10.88
    },
    "inT1" : {
        "average" : 22.78,
        "max" : 22.81,
        "min" : 22.75
    }
}

这篇关于MongoDB-一次汇总多个变量的最大/最小/平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆