【问题标题】:Node JS Mongo $near not working with nested fieldNode JS Mongo $near 不适用于嵌套字段
【发布时间】:2020-05-29 16:10:35
【问题描述】:

我一直在使用 $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...保持其他所有内容不变...
  • 太棒了!,请为我的回答点赞……:/

标签: node.js mongodb mongoose geojson


【解决方案1】:

$near 更改为$nearSphere,两者之间存在差异,主要与使用球面几何进行计算有关。 $near 将使用平面几何,并且由于嵌套文档上的 2dSphere 索引而无法工作。

看看这两者之间的区别......

In Mongo what is the difference between $near and $nearSphere?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多