I\'m having a problem applying a sort to an aggregation grouping. My raw data looks like the following:
{
\"_id\" : ObjectId(\"52deab2fe4b0a491
The fields you're sorting on are part of the _id
so you need to include that in your $sort
field names:
db.builds.aggregate([
{ $group: {
_id: {
month: { $month: "$time" },
day: { $dayOfYear: "$time" },
year: { $year: "$time" },
buildProjectName: "$data.buildProjectName",
},
buildDuration: { $avg: "$data.buildDuration" }
} },
{ $sort: {
'_id.buildProjectName': 1,
'_id.year': 1,
'_id.month': 1,
'_id.day': 1
} }
])