列出数据库中存储的纬度附近的纬度点附近的位置

时间:2018-12-04 14:46:10

标签: node.js mongodb google-maps gis geojson

我在中存储了[lat, lon]对不同的位置。现在,我想按距离比较某个坐标对,例如从该点以 2 km 的圆为单位,并希望从数据库中获取所有结果。

1 个答案:

答案 0 :(得分:0)

您应该看看geoNear命令。

通用示例如下:

db.runCommand(
   {
     geoNear: "places", // Your target collection
     near: { type: "Point", coordinates: [ -73.9667, 40.78 ] }, // Point coordinates
     spherical: true, // 2dsphere index required for using this option
     query: { yourField: "someFilterVale" }, // Additional regular filtering
     maxDistance: 2000 // Maximum distance in meters/radians
   }
)
  

minDistance也可用于查询

为此,您应该在集合中具有 2d 2dsphere 索引。

  

还有$geoNear聚合。

相关问题