Node JS Mongo $near 无法使用嵌套字段
Node JS Mongo $near not working with nested field
我一直在使用 $near 运算符进行简单搜索,但最近更改了我的数据,因此 2dsphere 位置索引现在位于嵌套字段 properties.location
而不仅仅是 location
中。我正在使用 Node JS mongo 驱动程序,当索引未嵌套在 properties
对象中时,使用以下查询搜索工作正常,但 return 现在什么也没有:
之前的工作查询:
dbObject.collection("scrapedTimes").find({
location: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [lngBound, latBound]
},
$minDistance: 0,
$maxDistance: 100000
}
}
})
当前查询return什么都没有:
dbObject.collection("scrapedTimes").find({
"properties.location": {
$near: {
$geometry: {
type: "Point" ,
coordinates: [lngBound, latBound]
},
$minDistance: 0,
$maxDistance: 100000
}
}
})
我在 Mongo Compass 中执行了相同的嵌套查询,它似乎工作正常所以我认为这可能是 Node JS 驱动程序的问题?我是 Mongo 的新手,所以这里可能缺少一些明显的东西,但我无法让 Node 获得 return 任何结果...
感谢您的时间和建议,
乔希
将$near
改为$nearSphere
,两者有区别,主要是球面几何计算。 $near
将使用平面几何图形,并且由于嵌套文档上的 2dSphere
索引而无法工作。
看看这两者之间的区别...
我一直在使用 $near 运算符进行简单搜索,但最近更改了我的数据,因此 2dsphere 位置索引现在位于嵌套字段 properties.location
而不仅仅是 location
中。我正在使用 Node JS mongo 驱动程序,当索引未嵌套在 properties
对象中时,使用以下查询搜索工作正常,但 return 现在什么也没有:
之前的工作查询:
dbObject.collection("scrapedTimes").find({
location: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [lngBound, latBound]
},
$minDistance: 0,
$maxDistance: 100000
}
}
})
当前查询return什么都没有:
dbObject.collection("scrapedTimes").find({
"properties.location": {
$near: {
$geometry: {
type: "Point" ,
coordinates: [lngBound, latBound]
},
$minDistance: 0,
$maxDistance: 100000
}
}
})
我在 Mongo Compass 中执行了相同的嵌套查询,它似乎工作正常所以我认为这可能是 Node JS 驱动程序的问题?我是 Mongo 的新手,所以这里可能缺少一些明显的东西,但我无法让 Node 获得 return 任何结果...
感谢您的时间和建议,
乔希
将$near
改为$nearSphere
,两者有区别,主要是球面几何计算。 $near
将使用平面几何图形,并且由于嵌套文档上的 2dSphere
索引而无法工作。
看看这两者之间的区别...