I use mongodb 2.6. I have stored my data in this form:
{
"_id" : "my-sensor-1",
"points": [
{ "timeStamp" : NumberLong("1453468362174"), "value" : 41 },
{ "timeStamp" : NumberLong("1453468483297"), "value" : 66 },
{ "timeStamp" : NumberLong("1453468485568"), "value" : 49 },
...
]
}
In order to aggregate the documents I make queries like this:
db.pointsTest.aggregate([
{ $match: { $and: [ {"points.timeStamp" : { $gt : 1453433925163}},
{"_id":"my-sensor-10"} ] } },
{"$unwind":"$points"},
{$group: {_id: "my-sensor-1","average":{$avg : "$points.value"}}}
])
{ "_id" : "my-sensor-1", "average" : 52 }
Result
I have stored the timestamp as milliseconds so every time I want to aggregate a specific time interval I have to change the bounds of timeStamp value.
How could I make the aggregation on a time period and group the results by gap intervals (i.e aggregate the average value from now() -1day GROUP by 1h)?
EDIT
I want to make something like this:
db.pointsTest.aggregate([
{ $match: { $and: [ {"points.timeStamp" : { $gt : 1453433925163, $lt : 1453555555555}}, {"_id":"my-sensor-10"} ] } }, {"$unwind":"$points"}, {$group: {_id: "my-sensor-1","average":{$avg : "$points.value"}, ???}}
])
and the result would be the average of this time interval grouped by 1h. Assuming that I want to aggregate the average values for every hour of the 31 of december:
timestamp of interval 31/12/2015 20:00:00, average: xyz
timestamp of interval 31/12/2015 21:00:00, average: xyz
At this moment in order to achieve that I have to split the time interval in 1hour intervals and make several requests to the database.
I.E using InfluxDB to do the same I do this:
"SELECT MEAN(value) From myMeasures where key='my-sensor-1' and time > now() - 1d GROUP BY time(1h)"