【问题标题】:MongoDB can't parse query (2dsphere): two conditionsMongoDB无法解析查询(2dsphere):两个条件
【发布时间】:2021-07-14 13:21:28
【问题描述】:

我的收藏中有以下对象:

{
   "_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 本身不支持“重叠”(仅在内部、相交和附近),根据上述定义,我应该返回 所有不完全在搜索区域内的重叠文档.

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

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

经过几次测试,我似乎无法对同一属性执行第二个过滤器。

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

谢谢

【问题讨论】:

    标签: mongodb mongodb-query pymongo geojson


    【解决方案1】:

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

    { key: { query1, query2 }}
    

    其中query1$geoIntersectsquery2$not,这不是一个有效的结构,您可以用$and 查询将它们都包装起来,如下所示:

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

    【讨论】:

    • 我使用 Java 进行开发,并使用以下方法创建了 Bson 过滤器:return Filters.and( Filters.geoIntersects(attributeName, geometry), Filters.not(Filters.geoWithin(attributeName, geometry)) ); 但是,使用上述过滤器调用 db.collection.find(...),我得到了错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多