mongodb - 内部结构地理空间索引

标签 mongodb indexing geospatial

我有一个集合名称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是基于GeoJSON格式,最好先有经度元素

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

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

By default, the index assumes you are indexing longitude/latitude and is thus configured for a [-180..180) value range.

The code assumes that you are using decimal degrees in (longitude, latitude) order. This is the same order used for the GeoJSON spec. Using (latitude, longitude) will result in very incorrect results, but is often the ordering used elsewhere, so it is good to double-check. The names you assign to a location object (if using an object and not an array) are completely ignored, only the ordering is detected.

关于mongodb - 内部结构地理空间索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7735905/

相关文章:

c# - 找出矩形内的长/纬度

ruby - 我可以在不坚持的情况下删除 Mongoid 中的嵌入式文档吗?

mongodb - 说 MongoDB 在写入操作级别上是原子的是什么意思?

python - PyMongo 和 toArray() 方法

Javascript - 排序后获取 'WILL BE' 关系的索引数组

python - 根据当前值更新 Pandas 数据帧值

具有稀疏复合索引错误 13311 的 MongoDB $near 地理空间查询

javascript - 未知运营商 : $and in MongoDB

Mysql-内部连接条件索引

php - Mysql 近距离查询