MongoDb aggregation Group by Date

前端 未结 3 2051
离开以前
离开以前 2021-02-08 00:57

I\'m trying to group by timestamp for the collection named \"foo\" { _id, TimeStamp }

db.foos.aggregate(
[
   {$group : { _id : new Date (Date.UTC({ $year : \'$T         


        
3条回答
  •  南旧
    南旧 (楼主)
    2021-02-08 01:59

    This is what I use in one of my projects :

       collection.aggregate(
          // group results by date
          {$group : {
            _id : { date : "$date" }
            // do whatever you want here, like $push, $sum...
          }},
    
          // _id is the date
          {$sort : { _id : -1}},                        
          {$orderby: { _id : -1 }})
        .toArray()
    

    Where $date is a Date object in mongo. I get results indexed by date.

提交回复
热议问题