我的集合中有以下对象:
{
"_id":"test123",
"footprint":{
"type":"Polygon",
"coordinates":[
[
[10, 30], [20, 45], [38, 38], [43, 38], [45, 30], [10, 30]
]
]
}
}
具有类型为"的索引;2dsphere";关于";足迹";属性
现在,我想实现地理空间查询";"重叠";,由PostGIS中的ST_Overlaps实现:https://postgis.net/docs/ST_Overlaps.html.
由于MongoDB不支持";"重叠";原生地(仅在内部、相交和附近(,根据上述定义,我将返回所有不完全在搜索区域内的重叠文档。
因此,我尝试执行以下过滤器:
{
"footprint":{
"$geoIntersects":{
"$geometry":{
"type":"Polygon",
"coordinates":[
[
[
41.62109375000001,
38.087716380862716
],
[
41.870727539062514,
37.998201197578084
],
[
41.72393798828124,
38.01268326428104
],
[
41.62109375000001,
38.087716380862716
]
]
]
}
},
"$not":{
"$geoWithin":{
"$geometry":{
"type":"Polygon",
"coordinates":[
[
[
41.62109375000001,
38.087716380862716
],
[
41.870727539062514,
37.998201197578084
],
[
41.72393798828124,
38.01268326428104
],
[
41.62109375000001,
38.087716380862716
]
]
]
}
}
}
}
}
但我得到以下错误:
can't parse extra field: $not: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [ 41.62109375000001, 38.08771638086272 ], [ 41.87072753906251, 37.99820119757808 ], [ 41.72393798828124, 38.01268326428104 ], [ 41.62109375000001, 38.08771638086272 ] ] ] } } }
经过几次测试,我似乎无法对同一属性执行第二个筛选器。
我错了吗?有什么变通办法吗?
感谢
这是由于查询语言及其解析对象的方式,您尝试使用的对象如下所示:
{ key: { query1, query2 }}
其中query1
是$geoIntersects
,query2
是$not
,这不是一个有效的结构,您可以用$和类似的查询来包装它们:
{
$and: [
{
"footprint": {
"$geoIntersects": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[
41.62109375000001,
38.087716380862716
],
[
41.870727539062514,
37.998201197578084
],
[
41.72393798828124,
38.01268326428104
],
[
41.62109375000001,
38.087716380862716
]
]
]
}
}
}
},
{
footprint: {
"$not": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[
41.62109375000001,
38.087716380862716
],
[
41.870727539062514,
37.998201197578084
],
[
41.72393798828124,
38.01268326428104
],
[
41.62109375000001,
38.087716380862716
]
]
]
}
}
}
}
}
]
}