【问题标题】:find geo-points inside area查找区域内的地理点
【发布时间】:2021-08-13 00:46:53
【问题描述】:

如何在 mongodb 中搜索一个区域内的所有地理点。

我的收藏有

{
    "_id": {
        "$oid": "5fa2a64d267b8309fe781d98"
    },
    "location": {
        "coordinates": ["115.880453987155", "-31.925513609342207"]
    }
}

我现在想搜索这些数据以查看用户是否在里面

顶部:-31.97127、115.98367 底部:-32.11739、115.85955

我已经尝试过运行

[
  {
    '$search': {
      'location': {
        '$geoWithin': {
          '$geometry': {
            'type': 'Polygon', 
            'coordinates': [
              [
                115.8455154208042, -31.97458396927722
              ], [
                115.8830653531429, -31.97460201459856
              ], [
                115.8823782261087, -31.94124526669114
              ], [
                115.8498438592383, -31.9409449398814
              ], [
                115.8455154208042, -31.97458396927722
              ]
            ], 
            'crs': {
              'type': 'name', 
              'properties': {
                'name': 'urn:x-mongodb:crs:strictwinding:EPSG:4326'
              }
            }
          }
        }
      }
    }
  }
]

但得到错误Remote error from mongot :: caused by :: Query should contain either operator or collector

https://mongoplayground.net/p/xwRyKoEXlBI

【问题讨论】:

    标签: mongodb geolocation


    【解决方案1】:
    1. 首先,您需要将集合中的坐标类型从字符串更改为数字,请参阅GeoJSON Objects 示例,
    {
        "_id": {
            "$oid": "5fa2a64d267b8309fe781d98"
        },
        "location": {
            "coordinates": [115.880453987155, -31.925513609342207]
        }
    }
    
    • 您可以尝试更新查询:
    • $arrayElemAt 从数组中获取特定元素
    • $toDouble 将值转换为双精度
    db.collection.updateMany(
      { "location.coordinates": { $exists: true } },
      [{
        $set: {
          "location.coordinates": [
            { 
              $toDouble: {
                $arrayElemAt: ["$location.coordinates", 0]
              }
            },
            { 
              $toDouble: {
                $arrayElemAt: ["$location.coordinates", 1]
              }
            }
          ]
        }
      }]
    )
    
    1. 创建2dsphere 索引:
      • 地理空间索引,几乎总能提高$geoWithin 和$geoIntersects 查询的性能。
    db.collection.createIndex({ location: "2dsphere" })
    
    1. 更正您的查询:
    db.collection.find({ 
      location: { 
        $geoWithin: { 
          $geometry: {
            "type" : "Polygon",
            "coordinates" : [
              [
                [115.8455154208042, -31.97458396927722],
                [115.8830653531429, -31.97460201459856],
                [115.8823782261087, -31.94124526669114],
                [115.8498438592383, -31.9409449398814],
                [115.8455154208042, -31.97458396927722]
              ]
            ]
          } 
        } 
      } 
    })
    

    我还没有测试所有的流程,请确保你应该克隆你的数据库并在测试集合中进行这种测试!

    【讨论】:

      【解决方案2】:

      如果您可以从地图中提取几何点以形成“多边形”,那么您可以使用 $geoWithin 搜索在其中搜索您的点: GeoWithin search

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2013-10-06
      • 1970-01-01
      相关资源
      最近更新 更多