我收藏了这样的文件:
{
...
"CurrentLocation" : {
"type" : "Point",
"coordinates" : [
-92.48436693078111,
35.85223020932276
]
}
...
}
我需要做一个" $ near"查询两个可能的中心点:
db.Truck.find({
"$or": [{
"DestinationLocation": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [-117.256875, 41.856405]
},
"$maxDistance": 100000.0
}
}
}, {
"DestinationLocation": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [-112.256875, 40.856405]
},
"$maxDistance": 100000.0
}
}
}]
})
Mongo给我一个错误:
error:
{
"$err" : "Can't canonicalize query: BadValue Too many geoNear expressions",
"code" : 17287
}
除了应用程序端的数据联合之外,有没有办法用两点来请求$ near? 感谢。
答案 0 :(得分:1)
Mongo DB只接受一个NEAR。如果有NEAR,它必须是根或根 必须是一个AND,它的孩子必须是一个NEAR。见https://github.com/mongodb/mongo/blob/master/src/mongo/db/query/canonical_query.cpp#L733
如果您需要使用两个$or
进行$near
操作,请尝试进行两次查询并整合其中的结果。
答案 1 :(得分:0)
带有$ or
的示例db.Truck.find({
"DestinationLocation": {
"$or":[{
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [-117.256875, 41.856405]
},
"$maxDistance": 100000.0
}
},{
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [-112.256875, 40.856405]
},
"$maxDistance": 100000.0
}
}]
}
})