MongoDB多级$lookup排序不工作

MongoDB多级$lookup排序不工作,mongodb,mongoose,mongodb-query,aggregation-framework,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,多级$lookup排序在聚合中不起作用 排序仅适用于国家、州名称。尝试对城市应用排序,但国家/地区排序覆盖了城市排序 Query2正在工作,但我不想在查找管道中对集合进行排序 有没有办法实现查询1中所有级别的排序(国家、州、城市) 查询1(不工作): Country.aggregate([ { $lookup:{ from: 'states', localFie

多级$lookup排序在聚合中不起作用

排序仅适用于国家、州名称。尝试对城市应用排序,但国家/地区排序覆盖了城市排序

Query2正在工作,但我不想在查找管道中对集合进行排序

有没有办法实现查询1中所有级别的排序(国家、州、城市)

查询1(不工作):

Country.aggregate([
            {
                $lookup:{
                    from: 'states',
                    localField:'_id',
                    foreignField:'countryId',
                    as:'states'
                }
            },
            {
                $unwind: {
                    path: "$states",
                    preserveNullAndEmptyArrays: true
                }
            },
            {
                $sort:  {
                    'states.name': 1
                }
            },
            {
                $lookup:{
                    from: 'cities',
                    localField:'states._id',
                    foreignField:'stateId',
                    as:'states.cities'
                }
            },
            {
                $sort:  {
                    'states.cities.name': 1
                }
            },
            {
                $group: {
                    _id: {
                        _id: '$_id',
                        name: '$name'
                    },
                    states: {
                        $push: '$states'
                    }
                }
            },
            {
                $project: {
                    _id: '$_id._id',
                    name: '$_id.name',
                    states: 1
                }
            }, 
            {
                $sort:  {
                    name: 1
                }
            }
        ])
查询2(工作): 执行时间比Query1高8倍

[
        {
            $lookup : {
                from : 'states',
                let: { 'countryId': '$_id' },
                pipeline: [
                    {
                        $match: {
                            $expr:
                                {
                                    $eq: ['$countryId', '$$countryId']
                                }
                            }
                        },
                    {
                        $sort : {
                            name : -1
                        }
                    }
                ],
                as : 'states'
            }
        },
        {
            $unwind: {
                path: '$states',
                preserveNullAndEmptyArrays: true
            }
        },
        {
            $lookup : {
                from : 'cities',
                let: { 'stateId': '$states._id' },
                pipeline: [
                    {
                        $match: {
                            $expr:
                                {
                                    $eq: ['$stateId', '$$stateId']
                                }
                            }
                        },
                    {
                        $sort : {
                            name : -1
                        }
                    }
                ],
                as : 'states.cities'
            }
        },
        {
            $group: {
                _id: {
                    _id: '$_id',
                    name: '$name'
                },
                states: {
                    $push: '$states'
                }
            }
        },
        {
            $project: {
                _id: '$_id._id',
                name: '$_id.name',
                states: 1
            }
        }
    ]
在较新的语法中,不需要使用来连接嵌套字段。您可以轻松地在管道内部使用来连接多个级别

[
  { "$lookup": {
    "from": "states",
    "let": { "countryId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": ["$countryId", "$$countryId"] }}},
      { "$lookup": {
        "from": "cities",
        "let": { "stateId": "$_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": ["$stateId", "$$stateId"] }}},
          { "$sort": { "name": -1 }}
        ],
        "as": "cities"
      }},
      { "$sort": { "name": -1 }}
    ],
    "as": "states"
  }}
]