【发布时间】: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