【问题标题】:Geospatial Index for inner structure内部结构的地理空间索引
【发布时间】:2011-12-05 20:38:43
【问题描述】:

我有一个集合名称locations,其数据结构如下:

{ 
  "_id" : ObjectId("4e95263f1783ae8487be26d4"),
  "name" : "test 1", 
  "location" : { 
     "coordinate" : { 
        "latitude" : 40.731987, 
        "longitude" : -73.999701
     },
     "address": "xxxxxxx"
  }
}

并且想要针对location.coordinate 字段进行地理查询。

当我尝试添加索引时,我得到以下结果:

$> db.locations.ensureIndex( { "location.coordinate" : "2d" } )
$> **** SyntaxError: syntax error (shell):0

这种结构可以使用地理空间索引吗?

【问题讨论】:

    标签: mongodb indexing geospatial


    【解决方案1】:

    由于mongodb是基于GeoJSON格式的,所以最好先有经度元素

    "location" : { 
         "coordinate" : {        
            "longitude" : -73.999701,
            "latitude" : 40.731987 
         },
    

    mongodb geospatial page,你可以在多个地方看到

    默认情况下,索引假定您正在索引经度/纬度和 因此配置为 [-180..180) 值范围。

    代码假定您在 (longitude, 纬度)顺序。这与 GeoJSON 规范使用的顺序相同。 使用 (latitude, longitude) 会导致非常不正确的结果,但是 通常是其他地方使用的排序,因此最好仔细检查。 您分配给位置对象的名称(如果使用对象而不是 一个数组)被完全忽略,只检测排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-30
      • 2013-05-27
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 2019-11-30
      相关资源
      最近更新 更多