【问题标题】:MongoDB can't parse query (2dsphere): Java APIMongoDB 无法解析查询(2dsphere):Java API
【发布时间】:2021-04-20 22:53:22
【问题描述】:

我的收藏中有以下对象:

{
   "_id":"test123",
   "footprint":{
      "type":"Polygon",
      "coordinates":[
         [
            [10, 30], [20, 45], [38, 38], [43, 38], [45, 30], [10, 30]
         ]
      ]
   }
}

在“footprint”属性上具有“2dsphere”类型的索引。

现在,我想实现地理空间查询“重叠”,由 PostGIS 中的 ST_Overlaps 实现:https://postgis.net/docs/ST_Overlaps.html

由于 MongoDB 本身不支持“重叠”(仅在内部、相交和附近),根据上述定义,我应该返回 所有不完全在搜索区域内的重叠文档.

使用 mongo-java-drivers 3.12.8,我开发了以下 Bson 过滤器:

        Polygon polygon = new Polygon(
                new PolygonCoordinates(
                        Arrays.asList(
                                new Position(41.62109375000001d, 38.087716380862716d), 
                                new Position(41.870727539062514d, 37.998201197578084d), 
                                new Position(41.72393798828124d, 38.01268326428104d), 
                                new Position(41.62109375000001d, 38.087716380862716d)
                        )
                )
        );
        Bson spatialFilter = Filters.and(
                    Filters.geoIntersects("footprint", polygon), 
                    Filters.not(Filters.geoWithin("footprint", polygon))
        );

但是当我执行以下操作时:

db.collection.find(spatialFilter);

我收到以下错误:

Query failed with error code 2 and error message 'can't parse extra field: $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [ 41.62109375000001, 38.08771638086272 ], [ 41.87072753906251, 37.99820119757808 ], [ 41.72393798828124, 38.01268326428104 ], [ 41.62109375000001, 38.08771638086272 ] ] ] } }' on server localhost:27017

正如这里MongoDB can't parse query (2dsphere): two conditions 所解释的,似乎“$and”包装过滤器没有正确生成。

我错了吗?有什么解决办法吗?

谢谢

【问题讨论】:

    标签: mongodb mongodb-query pymongo geojson


    【解决方案1】:

    也许,这是 3.12.8 版本中的一个错误。 它似乎在 4.2.3 版本中修复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      相关资源
      最近更新 更多