【问题标题】:MongoDB create 2dsphere indexes with array of features for spatial queryMongoDB 创建具有用于空间查询的特征数组的 2dsphere 索引
【发布时间】:2021-03-12 18:14:51
【问题描述】:

我想在 MongoDB 的“功能”字段上创建一个 2dsphere 索引。但它包含一组 GeoJSON LineStrings。如果没有这个数组,我只能为单个 GeoJSON LineString 创建 2dsphere 索引,例如db.collection.createIndex({ loc : "2dsphere" )。我想执行像collection.find({ $features: { $geometry: bounding_box } })这样的空间查询

有人知道如何为 GeoJSON 对象数组创建 2dsphere 吗?

{
    "_id" : ObjectId("..."),
    "name" : "street_segment_names",
    "features" : [ 
        {
            "type" : "Feature",
            "geometry" : {
                "type" : "LineString",
                "coordinates" : [ 
                    [.,.],[.,.],[.,.]
                ]
            },
            "properties" : {
                "speed_limit" : 20
            }
        }, 
        {
            "type" : "Feature",
            "geometry" : {
                "type" : "LineString",
                "coordinates" : [ 
                    [.,.],[.,.],[.,.]
                ]
            },
            "properties" : {
                "speed_limit" : 30
            }
        }
    ]
}

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    MongoDB 不支持将“Feature”作为 GeoJSON 对象类型。见https://docs.mongodb.com/manual/reference/geojson/#geojson-objects

    问题中显示的结构在特征对象中包含一个“几何”字段,该字段似乎是受支持的对象类型。

    您可以使用以下方法在该字段上创建索引:

    db.collection.createIndex({"features.geometry":"2dsphere"})
    

    根据您使用“边界框”查询的目标,您可以使用$geoWithin$geoIntersects,例如

    db.collection.find({
      "features.geometry":{
         $geoWithin: {
            $geometry: {
               type: <"Polygon" or "MultiPolygon"> ,
               coordinates: [ <coordinates> ]
            }
         }
      }
    })
    

    【讨论】:

    • 这有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2021-04-17
    • 2021-10-25
    • 1970-01-01
    • 2014-03-25
    • 2015-08-21
    • 2013-02-26
    相关资源
    最近更新 更多