【问题标题】:Apply filter on $geoWithin MongoDB在 $geoWithin MongoDB 上应用过滤器
【发布时间】:2017-12-15 02:23:56
【问题描述】:

我将所有点、线串和多边形存储在一个集合中。我可以使用 $geoWithin 检索给定多边形内的所有点和线串,如下所示:

db.BLR_all.find(
   {
       "geom" : {
          "$geoWithin" : {
              "$geometry" : {
                    "type" : "Polygon",
                    "coordinates" : [ [ /** array of points **/ ] ] 
                }
            }
        }
    })

如何修改上述查询以仅提取给定多边形中的点类型?

谢谢

【问题讨论】:

    标签: mongodb gis


    【解决方案1】:

    你的查询修改其实很简单,只需要在查询中加入 GeoJSON 类型的过滤条件即可:

    db.BLR_all.find(
       {
           "geom" : {
              "$geoWithin" : {
                  "$geometry" : {
                        "type" : "Polygon",
                        "coordinates" : [ [ /** array of points **/ ] ] 
                    }
                }
            },
            "geom.type": "Point"
        })
    

    或者特别是一些样本数据,

    {
        "_id" : ObjectId("534cb37acca5f311cc5c23cc"),
        "loc" : {
                "type" : "Polygon",
                "coordinates" : [
                        [
                                [
                                        0,
                                        0
                                ],
                                [
                                        3,
                                        6
                                ],
                                [
                                        6,
                                        1
                                ],
                                [
                                        0,
                                        0
                                ]
                        ]
                ]
        }
    }
    {
        "_id" : ObjectId("535aeddfb6b8fd04b3424a09"),
        "loc" : {
                "type" : "Point",
                "coordinates" : [
                        3,
                        6
                ]
        }
    }
    {
        "_id" : ObjectId("535aef22b6b8fd04b3424a0a"),
        "loc" : {
                "type" : "Point",
                "coordinates" : [
                        9,
                        12
                ]
        }
    }
    

    发出查询:

    db.geo.find({ 
        "loc": { 
            "$geoWithin": { 
                "$geometry": { 
                    "type": "Polygon", 
                    "coordinates":  [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ] 
                } 
            }
        }, 
        "loc.type": "Point" 
    })
    

    只会返回“点”且在坐标范围内的项目。

    {
        "_id" : ObjectId("535aeddfb6b8fd04b3424a09"),
        "loc" : {
                "type" : "Point",
                "coordinates" : [
                        3,
                        6
                ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-10
      • 2020-12-06
      • 2013-06-29
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2013-09-16
      相关资源
      最近更新 更多