node.js - Node JS Mongo $near 不适用于嵌套字段
问题描述
我一直在使用 $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
嵌套文档上的索引而无法工作。
看看这两者之间的区别......