【问题标题】:MongoDB can't parse query (2dsphere): $geoWithin:MongoDB 无法解析查询 (2dsphere): $geoWithin:
【发布时间】:2014-06-05 23:55:24
【问题描述】:

我的收藏中有以下对象,如下所示:

{
    "_id" : ObjectId("527d33a8623f6efd1c997440"),
    "location" : {
        "geometry" : {
            "type" : "Point",
            "coordinates" : [ 
                -78.4067, 
                37.26725
            ]
        },
        "type" : "Feature",
        "properties" : {
            "name" : "Something here"
        }
    },
    "name" : "Name of Object"
}

我有以下索引:

{
    "location.geometry" : "2dsphere"
}

我可以做到以下几点:

 db.myCollection.find({'location.geometry':{'$near':{'$geometry':{'type':"Point", 'coordinates': [-78.406700,37.267250]}, '$maxDistance' : 1000 }}})

但是,我可以不能执行以下操作:

db.myCollection.find( { 'location.geometry': { '$geoWithin': 
                                            { '$geometry' :
                                                { 'type' : "Polygon", 
                                                  'coordinates' :  [ [ -118.108006, 34.046072], [ -117.978230, 34.041521] , [ -117.987328,33.913645 ]]  } }
                                         } } )

当它返回错误时:

error: {
    "$err" : "can't parse query (2dsphere): { $geoWithin: { $geometry: { type: \"Polygon\", coordinates: [ [ -118.108006, 34.046072 ], [ -117.97823, 34.041521 ], [ -117.987328, 33.913645 ] ] } } }",
    "code" : 16535
}

我是否使用了 geoWithin 错误?不能用在这个索引上吗?

【问题讨论】:

    标签: mongodb pymongo geojson


    【解决方案1】:

    您为 $geowithin 查询提供的多边形不正确。根据 GeoJSON 定义,多边形需要具有相同的起点和终点。

    正确的查询是:

    db.myCollection.find( { 'location.geometry': 
                               { '$geoWithin': 
                                        { '$geometry' :
                                              { 'type' : "Polygon", 
                                                 'coordinates' :  [ 
                                                       [ -118.108006, 34.046072], 
                                                       [ -117.978230, 34.041521], 
                                                       [ -117.987328,33.913645 ],
                                                       [ -118.108006, 34.046072]
                                                    ]  
                                              } 
                                         }
                                 }
                           }
                         );
    

    注意更新后的坐标数组。

    显然,MongoDB 文档中提到的here 关于Polygons 的隐式连接 是不正确。它说当您在 MongoDB 中使用 $polygon 定义多边形时,只有这样连接才是隐式的。它没有说明智能并在提供给查询的 GeoJSON 多边形中建立隐式连接。

    事实上,如果对于某个 GeoJSON 变量,您说它的类型是多边形并且您没有将其开头与结尾连接起来,那么您一开始就没有创建正确的 GeoJSON 多边形。

    【讨论】:

      【解决方案2】:

      MongoDB 文档中有关 $geoWithin 查询的错误。虽然文档指出:

      指定的最后一个点总是隐式连接到第一个点。 您可以根据需要指定任意多的点和边。

      这是不正确的。多边形需要闭合。在 MongoDB Jira 中有一张关于此的公开票:

      https://jira.mongodb.org/browse/DOCS-2029

      所以你的第一个点和最后一个点需要相等 - 你不能依赖 MongoDB 隐式绘制多边形的最后一条线。

      【讨论】:

      • 感谢您澄清这一点。我现在知道不要相信手册
      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 2013-09-16
      • 2016-07-24
      • 2015-11-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      相关资源
      最近更新 更多