这个集合有两个地理字段:fromLocation和toLocation。但是只允许一个Geonear。
该集合的结构如下:
...............
fromLocation: {
type: { type: String, default: "Point" },
coordinates: [Number],
},
toLocation: {
type: { type: String, default: "Point" },
coordinates: [Number],
},
.........................................
请给出一个使用Geonear在两个字段中搜索的示例代码。
我现有的一种只能用于单个字段搜索的代码:
[
{
'$geoNear': {
near: [Object],
key: 'fromLocation',
distanceField: 'fromDistance',
spherical: true
}
},
{
'$match': {
status: 2,
'from.data.city_fias_id': '27c5bc66-61bf-4a17-b0cd-ca0eb64192d6',
'to.data.city_fias_id': '27c5bc66-61bf-4a17-b0cd-ca0eb64192d6',
'car.paymentInfo.id': [Object],
budget: [Object]
}
},
{
'$lookup': {
from: 'users',
localField: 'autor',
foreignField: '_id',
as: 'autor'
}
},
{ '$unwind': '$autor' },
{ '$addFields': { sortBudget: [Object] } },
{ '$sort': { sortBudget: 1 } },
{ '$group': { _id: null, total: [Object], results: [Object] } },
{ '$project': { total: 1, results: [Object] } }
]