我一直在使用 $near 操作符进行简单的搜索,但最近改变了我的数据,所以 2dsphere 位置索引现在在嵌套字段中。properties.location
而不是仅仅 location
. 我使用的是Node JS mongo驱动,当索引不嵌套在 properties
对象,但现在什么也没有返回。
前一个工作查询。
dbObject.collection("scrapedTimes").find({
location: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [lngBound, latBound]
},
$minDistance: 0,
$maxDistance: 100000
}
}
})
当前的查询什么都没有返回。
dbObject.collection("scrapedTimes").find({
"properties.location": {
$near: {
$geometry: {
type: "Point" ,
coordinates: [lngBound, latBound]
},
$minDistance: 0,
$maxDistance: 100000
}
}
})
我已经在Mongo Compass中执行了同样的嵌套查询,但似乎工作正常,所以我认为可能是Node JS驱动的问题?我对Mongo很陌生,所以我可能遗漏了一些明显的东西,但我就是不能让Node返回任何结果......。
谢谢你的时间和建议。
乔希
更改 $near
到 $nearSphere
,两者之间有区别,主要是涉及到使用球面几何体的计算。$near
将使用平面几何体,而不能使用的原因是 2dSphere
嵌套文档上的索引。
看看这两者之间的区别...