【问题标题】:mongodb and geospatial schemamongodb 和地理空间模式
【发布时间】:2013-12-31 11:24:03
【问题描述】:

我对 mongo 和地理空间感到头疼, 所以也许有人有一些想法或解决方案如何解决这个问题: 我的对象架构类似于从 http://geojson.org/geojson-spec.html 获取的 geoJSON 示例。

{
"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":{}}
]}}

附加信息:我正在使用弹簧数据,但这不应该影响答案。 主要问题是如何/在哪里将索引放在这个模式中。如果某些多边形相交,我需要进行查询以查找给定 Point 的所有文档。

提前致谢。

【问题讨论】:

    标签: mongodb geospatial


    【解决方案1】:

    通过在geoJSON.features.geometry 上创建 2d 或 2dsphere 索引,您应该能够创建涵盖所有 geoJSON 对象的索引。

    要获取features 数组中至少一个子对象覆盖某个点的所有文档,您可以将$geoIntersects 运算符与geoJSON 点一起使用:

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

    【讨论】:

    • 如果有帮助,我给你买啤酒。
    • 通过添加这样的索引: db..ensureIndex( { "geoJSON.features.geometry" : "2dsphere" }) 并使用此查询: db..find({ "geoJSON.features.geometry":{$geoIntersects:{$geometry :{type:"Point",coordinates:[100,0]}}}})
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多