根据数组 Mongoose/Mongodb 中嵌套子文档的 desc 值对查询结果进行排序 [英] Sort query results based desc value of nested subdocument within array Mongoose/Mongodb

查看:60
本文介绍了根据数组 Mongoose/Mongodb 中嵌套子文档的 desc 值对查询结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文档如下所示:

{
 { 
    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 查找文档的查询如下所示:

The query to find documents based on slugs within the subdocuments looks like this:

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

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

Note that I'd like to find agents by location slug and sort the result using totalClosedSales however I'm unable to get it to work. So the desired result would look like this:

{
  {
    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
    }
    ]
  },
}

推荐答案

我们不能直接对数组进行排序,但聚合有帮助

We can't sort array directly, But aggregation helps it

  • $unwind 有助于解构数组
  • $sort 有助于按照您的意愿进行排序
  • $group 有助于重新组合解构数组
  • $unwind helps to de-structure the array
  • $sort helps to sort as you wish
  • $group helps to re-group the de-structured array

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 游乐场

这篇关于根据数组 Mongoose/Mongodb 中嵌套子文档的 desc 值对查询结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆