如何查询"Car"集合,该集合为每辆汽车存储一个文档,并检索名称中包含特定关键字的所有汽车.然后,找到每辆车的最新"carinfo"文档,只返回距离给定坐标一定距离(X英里)内的车.
以下是相关集合的架构:
Car Schema:
{
"_id": "ObjectId",
"name": "String",
}
CarInfo Schema:
{
"car": {
"$ref": "Car"
},
"address": {
"location": {
"type": "Point",
"coordinates": [Number]
}
},
"timestamp": "Date",
}
我试过了,但我一直收到: MongoServerError:在此上下文中不允许使用$GeoNear、$NEAR和$NEAR球体
db.cars.aggregate([
{
$match: {
name: { $in: KEYWORDS},
},
},
{
$lookup: {
from: 'carinfos',
localField: '_id',
foreignField: 'car',
as: 'carInfo',
pipeline: [{ $sort: { timestamp: -1 } }, { $limit: 1 }],
},
},
{
$unwind: '$carInfo',
},
{
$match: {
'carInfo.address.location': {
$near: {
$geometry: {
type: 'Point',
coordinates: coordinates,
},
$maxDistance: 5 * 1609.34,
},
},
},
}
])