【问题标题】:Geospatial query doesn't work?地理空间查询不起作用?
【发布时间】:2017-12-05 15:34:45
【问题描述】:

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

{
    "_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"})

如果我执行查询:

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

我获得了所有记录,但我期望只有几条记录(靠近我的坐标!)

有什么建议吗? 提前致谢

【问题讨论】:

  • 对不起,使用 $near 的地理空间查询获取所有按距离以 desc 模式排序的记录!

标签: mongodb geospatial


【解决方案1】:

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

find() 函数中你应该有这样的东西。

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

【讨论】:

    猜你喜欢
    • 2015-06-07
    • 2013-09-25
    • 2018-04-06
    • 1970-01-01
    • 2012-11-29
    • 2018-03-20
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多