MongoDb sum query

As llovet suggested, the aggregation framework is the way to go. Here’s what your query would look like:

db.CollectionNameGoesHere.aggregate({ $match: {
    $and: [
        { hour: { $gte: 11 } },
        { hour: { $lte: 12 } }
    ]
} },
{ $group: { _id : null, sum : { $sum: "$incoming" } } });

You can also shape the resulting document to only contain the sum by adding a $project operator at the end of the pipeline, like so:

{ $project: { _id: 0, sum: 1 } }

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)