首页 文章

MongoDB $ geoNear操作“近”字段用法

提问于
浏览
0

我有一个带文件的集合“机器”

{
        "_id" : "ac9d1db9-6a0d-47c6-97d3-a613c8dd0031",
        "pin" : {
            "machine":"test1",
            "position" : [ 
                -1.5716, 
                54.7732
            ]
        }
    }

Note: -1.5716 is lng and 54.7732 is lat

我在文档上创建了一个2dsphere索引

db.machine.createIndex( { 'pin.position' : "2dsphere" } )

我尝试使用2个不同版本的查询( only difference in query is the near field in geoNear pipeline stage

Query 1:

db.machine.aggregate(
      [
       {
           "$geoNear":{
               "near": [-0.2129092,51.5031594],
               "limit":100,
               "maxDistance":500*1000,
               "distanceField": "dist.calculated",
               "includeLocs": "dist.location",
               "distanceMultiplier":1/1000,
               "spherical": true
        }

       }
      ]
    )

Note: -0.2129092 is lng and 51.5031594 is lat

Query 2

db.machine.aggregate(
      [
       {
           "$geoNear":{
               "near": { type: "Point", coordinates: [-0.2129092,51.5031594] },
               "limit":100,
               "maxDistance":500*1000,
               "distanceField": "dist.calculated",
               "includeLocs": "dist.location",
               "distanceMultiplier":1/1000,
               "spherical": true
        }

       }
      ]
    )

Note: -0.2129092 is lng and 51.5031594 is lat

查询1将文档返回给我,并提供此文档距离搜索坐标5.88161133560063e-05 Kms

查询2返回文档,并提供此文档距离搜索坐标375.135052595944 Kms

我交叉验证了网站http://andrew.hedges.name/experiments/haversine/上这些lng / lat之间的距离,并看到文档和搜索坐标之间的距离大约是374.835 Kms

似乎查询2提供了正确的结果,但我不确定查询1和查询2之间的区别是什么以及我是否正确使用它 .

请指教

1 回答

相关问题