MongoDB aggregation sort not working

前端 未结 2 1486
一生所求
一生所求 2021-02-13 03:44

I\'m having a problem applying a sort to an aggregation grouping. My raw data looks like the following:

    {
            \"_id\" : ObjectId(\"52deab2fe4b0a491         


        
2条回答
  •  无人共我
    2021-02-13 04:25

    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
        } }
    ])
    

提交回复
热议问题