javascript - Mongoose aggregation not sorting dates -


can me understand why aggregation query not sorting results created_at date?

user.aggregate([  // records created in last 30 days  { $match: {   "created_at":{ $gt: new date(today.gettime() - 1000*60*60*24*30) }  }},  //sort results signup date  { $sort : {   "created_at" : -1 }  },  // year, month , day createdtimestamp  { $project: {   "year":{ $year:"$created_at" },   "month":{ $month:"$created_at" },   "day": { $dayofmonth:"$created_at" }  }},  // group year, month , day , count  { $group: {   _id:{    year:"$year",    month:"$month",    day:"$day"   },   "count":{    $sum:1   }  }} ]) 

why aggregation query not sorting results created_at date?

there no guarantee $group being stable (i.e.: maintain order if possible).

so, if need result in particular order, should use $sort step last step of pipeline. (untested):

user.aggregate([  // records created in last 30 days  { $match: {   "created_at":{ $gt: new date(today.gettime() - 1000*60*60*24*30) }  }},  // year, month , day createdtimestamp  { $project: {   "year":{ $year:"$created_at" },   "month":{ $month:"$created_at" },   "day": { $dayofmonth:"$created_at" }  }},  // group year, month , day , count  { $group: {   _id:{    year:"$year",    month:"$month",    day:"$day"   },   "count":{    $sum:1   }  }},   //sort results year-month-day of signup date  { $sort : {   "year" : -1,   "month": -1,   "day": -1 }  } ]) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -