因此,如果我们使用这样的最大距离运行地理空间mongdodb查询:
db.getCollection('Places').aggregate([{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [-0.1251485, 51.5174914]
},
"spherical": true,
"distanceField": "dist",
"maxDistance": 2000,
"query": {
"field": "xxx"
}
}
}
}])
作为一个例子,我们得到了以下结果:
[PlaceA, PlaceB, PlaceC]
然后,假设我们运行以下查询:
db.getCollection('Places').aggregate([{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [-0.1251485, 51.5174914]
},
"spherical": true,
"distanceField": "dist",
"maxDistance": 2000,
"query": {
"field": "xxx"
}
}
},
{
$and: {
name: {
$eq: 'PlaceA'
}
},
}
])
假设我们试图在这个查询中得到
PlaceA
。如果我错了,请更正,但MongoDB不会先应用地理查询,然后选择
PlaceA, PlaceB, PlaceC
并筛选出PlaceB and PlaceC
。换句话说,$and
只是作为一个过滤器,我们可以使用for循环进行相同的过滤,而不是将其放入mongodb查询:for (eachPlace in Places) {
if (eachPlace == 'PlaceA') {
return eachPlace;
}
}
最佳答案
您可以将所有筛选器放入查询中,而不需要另一个管道
db.getCollection('Places').aggregate([{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [-0.1251485, 51.5174914]
},
"spherical": true,
"distanceField": "dist",
"maxDistance": 2000,
"query": {
"field": "xxx",
"name": {"$eq": 'PlaceA'}
}
}
}
])