内部结构的地理空间索引 [英] Geospatial Index for inner structure

查看:202
本文介绍了内部结构的地理空间索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a collections names locations with data structures like:

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

并希望针对 location.coordinate 字段进行地理位置查询。

and want to make geo queries against location.coordinate field.

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

When I'm trying to add index I get following results:

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

是否可以使用地理空间索引这样的结构?

Is it possible to use geospatial index for such structure?

推荐答案

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

Since mongodb is based on GeoJSON format, its better to have the longitude element first

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

mongodb地理空间页面,你可以看到在多个地方

In the mongodb geospatial page, you can see that in multiple places


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

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


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

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.

这篇关于内部结构的地理空间索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆