MongoDB aggregation - $group by date even if doesn't exist
问题 I have a query already which looks like this: {$match:{ "when":{$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 30)} }}, {$project:{ "year":{$year:"$when"}, "month":{$month:"$when"}, "day": {$dayOfMonth:"$when"} }}, {$group:{ _id:{year:"$year", month:"$month", day:"$day"}, "count":{$sum:1} }}, {$sort:{ _id: 1 }} The result looks like this: { "_id" : { "year" : 2015, "month" : 10, "day" : 19 }, "count" : 1 } { "_id" : { "year" : 2015, "month" : 10, "day" : 21 }, "count" : 2 } How