MongoDB 地理空间查询没有 return 任何东西

MongoDB geospatial query does not return anything

我有一个带有 2dsphere 索引的集合,我想使用 $geoWithin$centerSphere 函数来查询它。

模型摘录:

{
// ...
coordinates: {
    coordinates: {
        type: Array,
        index: '2dsphere'
    }
}
// ...
}

我成功地将一堆文档插入数据库,现在尝试通过以下方式查询:

Model.find({
    coordinates: {
        coordinates: {
            $geoWithin: {
                $centerSphere : [ [ lng, lat], r ]
            }
        }
    }
})

我面临的问题是,调用没有 return 任何东西,既不是错误,也不是数据。我在 mongo shell 以及我的 node.js 应用程序中尝试了它,我可以确认对象的经度和纬度已插入到数据库中。我还尝试将 $near$maxDistance 一起使用,结果相同。我还切换了纬度和经度只是为了检查......我确实通过除以地球半径将 r 转换为弧度。

我不知道现在出了什么问题,感谢您的任何建议!

我认为您应该更改模型,但请改用 "dot notation"

Model.find({
    "coordinates.coordinates": {
        "$geoWithin": {
            "$centerSphere" : [ [ lng, lat], r ]
        }            
    }
},function(err,callback) {

});