mongodb and geospatial schema

為{幸葍}努か 提交于 2019-12-23 03:50:46

问题


im breaking my head with mongo and geospatial, so maybe someone has some idea or solution how to solve this: my object schema is like this sample for geoJSON taken from http://geojson.org/geojson-spec.html.

{
"name":"name",
"geoJSON":{
"type":"FeatureCollection",
"features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":{}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":{}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":{}}
]}}

additional info: I'm using spring data but that shouldn't influence the answer. main problem is how/where to put indexes in this schema. I need to make a query to find all documents for given Point if some polygon intersects.

thanks in advance.


回答1:


By creating a 2d or 2dsphere index on geoJSON.features.geometry you should be able to create an index covering all of the geoJSON-objects.

To get all documents where at least one of the sub-object in the features array covers a certain point, you can use the $geoIntersects operator with a geoJSON Point:

db.yourcollection.find( 
            { `geoJSON.features.geometry` :
              { $geoIntersects :
                { $geometry :
                  { type : "Point" ,
                    coordinates: [ 100.5 , 0.5 ] 
                  } 
                } 
              } 
            } 
          )


来源:https://stackoverflow.com/questions/20553850/mongodb-and-geospatial-schema

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