1

I have a collection which contain document like:

{ _id: ObjectID(), userName: "aa", amount: 50, date: 1561119425317 }

The values which I have for the query are timeStamp1 and timeStamp2.

I want sum of the amount grouped by userName then the query for that is

db.collection.aggregate([
{$match: {date: {$gte: timeStamp1, $lte: timeStamp2}},
{$group: {"_id" : "$userName", total:{"$sum" : "$amount"}}}
])

I want the result in parts like I have broken the interval of timeStamp1 to timeStamp2 in 5 parts and then I want the same result for each interval.

How can i do that without running the above query for 5 times (one for each interval). Cause in future the number of interval might increase to 30 or 40.

Digvijay Rathore
  • 637
  • 1
  • 6
  • 21

1 Answers1

0

This seems like a case for $bucket

I can't type down an aggregation example right now, but the documentation for that pipeline stage is quite good.