From reading various articles out there, I believe this should be possible, but I'm not sure where exactly to start.
This is what I'm trying to do:
I want to run a query, where it finds all documents createdate within the last 24 hours, and groups all of them by hour, and since each hour document has a AVG "PM10" value and last 24 hours MAX_PM10 value,last 24 hours AVG_PM10_value.I want to run a query Iam getting each hour AVG_PM10 value remining how to calculate each hour avg_PM10 value and total_PM10_avg value ,toatal_PM10_max value please any one suggest me
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