I have a mongodb with entries like this:
{
time: Date
temp: Float
}
Now, one of these entries are generated once every 5 seconds and I want to get the average for each of these 20 minutes using node.js. The same goes for 10 last hours and 7 last days, but I assume once you solve minutes you can just copy and alter the rest.
E.g. I want an array with 20 entries which looks like this:
[[12:26, Average from that minute], [12:25, Average from that minutes], etc.]
Thanks.
EDIT: I tried following the links you kindly shared with me, but none of them solved my problem. Here is the code I used:
db.collection('temp').aggregate([
{ "$group": {
"time": {
"year": { "$year": "$time" },
"dayOfYear": { "$dayOfYear": "$time" },
"interval": {
"$subtract": [
{ "$minute": "$time" },
{ "$mod": [{ "$hour": "$time" }, 1 ]}
]
}
},
"count": { "$sum": 10 }
}}
]).each(function (err, doc) {
console.log(doc || err);
db.close();
});
This simply returns:
{ [MongoError: exception: unknown group operator 'year']
name: 'MongoError',
message: 'exception: unknown group operator \'year\'',
errmsg: 'exception: unknown group operator \'year\'',
code: 15952,
ok: 0 }