【问题标题】:MongoDB Multi level $lookup sort not workingMongoDB多级$查找排序不起作用
【发布时间】:2019-12-05 23:31:59
【问题描述】:

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

排序仅适用于国家、州名。尝试对城市应用排序,但国家排序会覆盖城市排序。

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

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

查询 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
            }
        }
    ]

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    在较新的$lookup 语法中,您不需要使用$unwind 来连接嵌套字段。您可以在管道内轻松使用$lookup 来加入多个级别。

    [
      { "$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"
      }}
    ]
    

    【讨论】:

      猜你喜欢
      • 2015-05-19
      • 2015-02-12
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 2019-11-10
      相关资源
      最近更新 更多