MongoDB无法解析查询(2dsphere):两个条件 [英] MongoDB can't parse query (2dsphere): two conditions

查看:83
本文介绍了MongoDB无法解析查询(2dsphere):两个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏夹中有以下对象:

I have the following object in my Collection:

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

索引类型为"2dsphere"的

在足迹"上属性.

with index of type "2dsphere" on "footprint" attribute.

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

Now, I would like to implements the geospatial query "overlaps", as implemented by ST_Overlaps in PostGIS: https://postgis.net/docs/ST_Overlaps.html.

由于MongoDB不支持重叠".本机(仅在内部,相交和附近)并且根据上述定义,我应该返回所有重叠的文档,而这些文档并不完全在搜索区域之内.

Due to the fact that MongoDB doesn't support "overlap" natively (only within, intersect and near) and according to the above definition, I whould return all overlapping documents not totally within the search area.

因此,我正在尝试执行以下过滤器:

Therefore, I'm trying to execute the following filter:

{
   "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 ] ] ] } } }

经过几次测试,看来我无法对同一属性执行第二个过滤器.

After several tests, it seems I can't execute a second filter on the same attribute.

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

Am I wrong? Is there any workaround?

谢谢

推荐答案

这是由于查询语言及其解析对象的方式所致,您尝试使用的对象如下所示:

This is due to the query language and how it parses objects, the object you're trying to use looks like this:

{ key: { query1, query2 }}

其中 query1 $ geoIntersects query2 $ not 的地方,这不是有效的结构,您可以使用 $ and 查询将它们都包装起来像这样:

Where query1 is $geoIntersects and query2 is $not which is just not a valid structure, what you can do is wrap both of them with an $and query like so:

{
    $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
                                    ]
                                ]
                            ]
                        }
                    }
                }
            }
        }
    ]
}

这篇关于MongoDB无法解析查询(2dsphere):两个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆