【发布时间】: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
}
]
},
}
【问题讨论】: