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

List locations near by to a lat, lon point from stored lat, lon in database

我在 MongoDb 中存储了 [lat, lon] 对不同的位置。现在我想按距离比较某个坐标对,比如从那个点开始 2 公里 的圆,并希望从数据库中获取所有结果。

你应该看看 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 is also available for querying

为此,您的 collection.

中应该有 2d or 2dsphere 索引

Also there is a $geoNear aggregation.