0

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 }
Fredrik
  • 971
  • 8
  • 23
  • possible duplicate of [Group result by 15 minutes time interval in MongoDb](http://stackoverflow.com/questions/26814427/group-result-by-15-minutes-time-interval-in-mongodb) – Blakes Seven Jul 27 '15 at 10:31
  • Check these aggregation operators: http://docs.mongodb.org/manual/reference/operator/aggregation-date/ – Michal Dymel Jul 27 '15 at 10:39

0 Answers0