地理空间查询不起作用?

时间:2017-12-05 15:34:46

标签: mongodb geospatial

我有一个包含许多文档的集合,其结构是:

{
    "_id" : ObjectId("5a26b07414e7d436786cfe07"),
    "location" : {
        "coordinates" : [ 
            -30.0000019073486, 
            25.0000019073486
        ],
        "type" : "Point"
    },
    "refDate" : ISODate("2017-11-23T05:00:00.000Z"),
    "hourProg" : 0,
    "value1" : 1,
    "value2" : 4
}

经度范围是[-30; 48]

纬度范围是[25; 55]

该集合包含约600,000条记录。

我设置了2d_sphere索引:db.myCollection.createIndex({location:“2dsphere”})

如果我执行thw查询:

db.myCollection.find( { 
    location : { 
        $near : {
            $geometry : {
                type : "Point" ,
                coordinates : [-25.27, 25.44] 
            }
        }
    }
}).count()

我获得了所有记录,但我只期待一些记录(靠近我的坐标!)

有什么建议吗? 提前致谢

1 个答案:

答案 0 :(得分:0)

可能的问题是因为您没有设置最大距离。

find()内部,你应该有类似的东西。

{
   <location field>: {
     $near: {
       $geometry: {
          type: "Point" ,
          coordinates: [ <longitude> , <latitude> ]
       },
       $maxDistance: <distance in meters>,
     }
   }
}
相关问题