Mongodb 内部结构的地理空间索引

Mongodb 内部结构的地理空间索引,mongodb,indexing,geospatial,Mongodb,Indexing,Geospatial,我有一个集合名称位置,数据结构如下: { "_id" : ObjectId("4e95263f1783ae8487be26d4"), "name" : "test 1", "location" : { "coordinate" : { "latitude" : 40.731987, "longitude" : -73.999701 }, "address": "xxxxxxx" } } 并希望对位置.坐标字

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

{ 
  "_id" : ObjectId("4e95263f1783ae8487be26d4"),
  "name" : "test 1", 
  "location" : { 
     "coordinate" : { 
        "latitude" : 40.731987, 
        "longitude" : -73.999701
     },
     "address": "xxxxxxx"
  }
}
并希望对
位置.坐标
字段进行地理查询

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

$> db.locations.ensureIndex( { "location.coordinate" : "2d" } )
$> **** SyntaxError: syntax error (shell):0
有可能对这种结构使用地理空间索引吗?

因为mongodb基于格式,所以最好先使用经度元素

"location" : { 
     "coordinate" : {        
        "longitude" : -73.999701,
        "latitude" : 40.731987 
     },
在中,您可以在多个地方看到这一点

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

代码假定您使用的是十进制度数(经度, 纬度)顺序。这与GeoJSON规范使用的顺序相同。 使用(纬度、经度)将导致非常不正确的结果,但是 通常在其他地方使用订单,因此最好仔细检查。 指定给位置对象的名称(如果使用对象而不是 完全忽略数组),仅检测排序