查询过去24小时内创建的所有文档,并按小时查询?

时间:2018-04-26 06:27:38

标签: mongodb mongoose aggregation-framework

从阅读各种文章,我相信这应该是可能的,但我不确定从哪里开始。

这就是我要做的事情:

我想运行一个查询,在那里查找过去24小时内创建的所有文档,并按小时对所有文档进行分组,并且因为每小时文档具有AVG“PM10”值并且最后24小时MAX_PM10值,最后24小时AVG_PM10_value.I想运行一个查询Iam得到每小时AVG_PM10值提醒如何计算每小时avg_PM10值和total_PM10_avg值,toatal_PM10_max值请任何一个建议我

Here's a sample of the collection:

    {
        "_id" : ObjectId("5ad768309ee4f03480792b20"),
        "CREATEDATE" : ISODate("2018-04-25T15:45:52.779Z"),
        "PM10" : 176.024749755859,
        "PM2DOT5" : 60.0020217895508,
    }
 {
        "_id" : ObjectId("5ad768309ee4f03480792b30"),
        "CREATEDATE" : ISODate("2018-04-25T16:45:52.779Z"),
        "PM10" : 176.024749755859,
        "PM2DOT5" : 60.0020217895508,
    }

And the aggregation query:

    db.collection1.aggregate(
     {$match: {
       CREATEDATE: {$gt: new Date(new Date(ISODate().getTime() - 1000*60*60*24))}
     }},
     {$project: {  "h":{"$hour":"$CREATEDATE"}, "PM10":1 }},
     {$group:{ 
       "_id":"$h",
       "Max_PM10": {"$max":"$PM10"},
       "PM10": {$avg:"$PM10"} 
     }}
   ])
Expected output:

    {
        "Hour":15,
        "AVG_PM10_VALUE":154.64,

    }
   {
        "Hour":16,
        "AVG_PM10_VALUE":157.64,

    }


  "TOTAL_PM10_MAX_VALUE":157.89,
  "TOTAL_PM10_AVG_VALUE":156.14

0 个答案:

没有答案