【问题标题】:Sort query results based desc value of nested subdocument within array Mongoose/Mongodb基于数组Mongoose/Mongodb中嵌套子文档的desc值对查询结果进行排序
【发布时间】:2023-03-16 02:14:01
【问题描述】:

我的文档如下所示:

{
 { 
    mlsId: 'RTC749',
    firstName: 'Tommy',
    lastName: 'Davidson',
    officeMlsId: 'RTC2421',
    officeName: 'John Jones Real Estate LLC',
    slug: 'tommy-davidson',
    serviceAreas: [
    {
      name: 'Nashville',
      slug: 'nashville',
      type: 'city',
      totalClosedSales: 3
    },
    {
      name: 'Franklin',
      slug: 'franklin',
      type: 'city',
      totalClosedSales: 7
    }
  },
 {
    id: 'RTC7280',
    firstName: 'Jack',
    lastName: 'Miller',
    slug: 'jack-miller',
    serviceAreas: [
    {
      name: 'Nashville',
      slug: 'nashville',
      type: 'city',
      totalClosedSales: 4
    },
    {
      name: 'Franklin',
      slug: 'franklin',
      type: 'city',
      totalClosedSales: 10
    }
  ]
 },
}

根据子文档中的 slug 查找文档的查询如下所示:

    const localAgents = await Agent.find(
      {
        'serviceAreas.slug': locationSlug,
      },
      '-_id -__v'
    )
      .sort({ 'serviceAreas.totalClosedSales': -1 })

请注意,我想按位置 slug 查找代理并使用totalClosedSales 对结果进行排序,但是我无法让它工作。所以想要的结果应该是这样的:

{
  {
    id: 'RTC7280',
    firstName: 'Jack',
    lastName: 'Miller',
    slug: 'jack-miller',
    serviceAreas: [

    {
      name: 'Franklin',
      slug: 'franklin',
      type: 'city',
      totalClosedSales: 10
    },
    {
      name: 'Nashville',
      slug: 'nashville',
      type: 'city',
      totalClosedSales: 4
    }

]
  },

 { 
    mlsId: 'RTC749',
    firstName: 'Tommy',
    lastName: 'Davidson',
    officeMlsId: 'RTC2421',
    officeName: 'John Jones Real Estate LLC',
    slug: 'tommy-davidson',
    serviceAreas: [
    {
      name: 'Nashville',
      slug: 'nashville',
      type: 'city',
      totalClosedSales: 3
    },
    {
      name: 'Franklin',
      slug: 'franklin',
      type: 'city',
      totalClosedSales: 7
    }
    ]
  },
}

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    我们不能直接对数组进行排序,但是聚合可以帮助它

    • $unwind 有助于解构数组
    • $sort 帮助您按需要排序
    • $group 有助于对解构数组进行重新分组

    Mongo 脚本如下

    db.collection.aggregate([
      {
        "$match": {
          "serviceAreas.slug": "nashville"
        }
      },
      {
        $unwind: "$serviceAreas"
      },
      {
        $sort: {
          "serviceAreas.totalClosedSales": -1
        }
      },
      {
        $addFields: {
          total: "$serviceAreas.totalClosedSales"
        }
      },
      {
        $sort: {
          total: -1
        }
      },
      {
        $group: {
          _id: "$_id",
          mlsId: {
            $first: "$mlsId"
          },
          firstName: {
            $first: "$firstName"
          },
          lastName: {
            $first: "$lastName"
          },
          slug: {
            $first: "$slug"
          },
          serviceAreas: {
            $push: "$serviceAreas"
          }
        }
      }
    ])
    

    工作Mongo playground

    【讨论】:

    • 我不想自己对数组进行排序。我的目标是按子文档的值 (totalClosedSales) 对整个结果进行排序。
    • 所以你只需要对与 ''serviceAreas.slug': locationSlug' 匹配的特定文档进行排序吗?这里的 locationSlug 是什么?
    • locationSlug 是子文档上的 slug 键。所以在这个例子中,它将是一个查询“富兰克林”
    • mongoplayground.net/p/lXtABNdq-nL 对您有帮助吗?
    • 您的查询会对数组本身进行排序。我需要的查询应该简单地根据数组中子文档的值对结果进行排序(目前不这样做)
    猜你喜欢
    • 2016-06-28
    • 2013-09-30
    • 2014-05-10
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 2017-01-12
    • 2021-05-06
    相关资源
    最近更新 更多