Sunday, 2 April 2017

Mongoose aggregate orders in an hour

I have a Mongoose Model called 'Order' that keeps track of user orders.

I want to group the orders by hour, and find how many orders were placed each hour, for the current day.

I have attempted to use the aggregation framework built into mongoose but cant figure out how to achieve this.

Could anyone give me an example of how this would work?

Ideally I want an output like the following

{0: 2, 1: 5, 2:4,...,23:2}

where the key is the hour, and the value is the no. of orders.

My initial attempt

Order.aggregate([
    {"$match":{
        "created_on" : {
            $gt: //early hour,
            $lt: //late hour
        }

    }},
    { "$group": { _id: null, count: { $sum: 1 } } }
], function(err, results){
    if(err) throw err;
    console.log(results);
});



via user2976358

No comments:

Post a Comment