Geospatial Index for inner structure

泄露秘密 提交于 2019-12-12 20:42:57

问题


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"
  }
}

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?


回答1:


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

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

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

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

and

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.



来源:https://stackoverflow.com/questions/7735905/geospatial-index-for-inner-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!