博客
关于我
mongodb-地理坐标存储查询
阅读量:803 次
发布时间:2023-02-09

本文共 2345 字,大约阅读时间需要 7 分钟。

MongoDB 地理搜索操作符详解

MongoDB 提供了丰富的地理搜索功能,能够支持复杂的地理查询场景。以下将详细介绍 MongoDB 的地理搜索操作符及其使用方法。

1. 地理查询操作符

$geoWithin 查询

$geoWithin 用于查询多边形范围内的点,替代了已被废弃的 $within 查询。通过该操作符,可以高效地筛选位于特定多边形范围内的文档。

示例:

db.places.find({  loc: {    $geoWithin: {      $geometry: {        type: "Polygon",        coordinates: [          [ [0, 0], [3, 6], [6, 1], [0, 0] ]        ]      }    }  }})

CRs 参数

对于涉及大于单个半球的查询,需要添加 CRs(坐标参考系统)参数。CRs 参数用于指定坐标系,确保查询结果的精度。

示例:

db.places.find({  loc: {    $geoWithin: {      $geometry: {        type: "Polygon",        coordinates: [          [ [-100, 60], [-100, 0], [-100, -60], [100, -60], [100, 60], [-100, 60] ],        ],        crs: {          type: "name",          properties: {            name: "urn:x-mongodb:crs:strictwinding:EPSG:4326"          }        }      }    }  }})

$geoIntersects 查询

$geoIntersects 用于查询几何对象与给定几何对象的交集。该操作符适用于需要判断文档是否与其他几何对象有交集的情况。

示例:

db.places.find({  loc: {    $geoIntersects: {      $geometry: {        type: "Polygon",        coordinates: [ [0, 0], [3, 6], [6, 1], [0, 0] ]      }    }  }})

2. 空间距离查询

$near 查询

$near 用于返回与给定点距离在指定范围内的文档。该操作符适用于需要查找附近位置的场景。

示例:

db.places.find({  location: {    $near: {      $geometry: {        type: "Point",        coordinates: [ -73.9667, 40.78 ]      },      $minDistance: 1000,      $maxDistance: 5000    }  }})

$nearSphere 查询

$nearSphere 用于在球体上执行近似查询。该操作符适用于需要查找球体表面附近位置的场景。

示例:

db.places.find({  location: {    $nearSphere: {      $geometry: {        type: "Point",        coordinates: [ -73.9667, 40.78 ]      },      $minDistance: 1000,      $maxDistance: 5000    }  }})

3. 创建空间索引

为了确保地理查询的高效性,必须在执行地理查询操作符之前创建相应的空间索引。以下是创建常用索引的示例:

示例:

db.places.ensureIndex({  loc: "2d"})

4. 其他查询操作符

$center 查询

$center 查询用于查找位于指定圆心的文档。

示例:

db.places.find({  loc: {    $geoWithin: {      $center: [ [ -74, 40.74 ], 10 ]    }  }})

$centerSphere 查询

$centerSphere 查询用于球体表面查找。

示例:

db.places.find({  loc: {    $geoWithin: {      $centerSphere: [ [ -88, 30 ], 10/3963.2 ]    }  }})

$box 查询

$box 查询用于查找位于矩形区域的文档。

示例:

db.places.find({  loc: {    $geoWithin: {      $box: [ [0, 0], [100, 100] ]    }  }})

$polygon 查询

$polygon 查询用于查找位于多边形区域的文档。

示例:

db.places.find({  loc: {    $geoWithin: {      $polygon: [ [0, 0], [3, 6], [6, 0] ]    }  }})

5. 空间索引的重要性

创建空间索引是使用 MongoDB 地理查询功能的前提条件。以下是创建常见索引的示例:

示例:

db.places.createIndex({  loc: "2d"})

通过以上方法,您可以充分利用 MongoDB 的地理搜索功能,高效地执行复杂的地理查询任务。

转载地址:http://ujffk.baihongyu.com/

你可能感兴趣的文章
Pinpoint对Kubernetes关键业务模块进行全链路监控
查看>>
Pinterest 大规模缓存集群的架构剖析
查看>>
pintos project (2) Project 1 Thread -Mission 1 Code
查看>>
PinYin4j库的使用
查看>>
PIP
查看>>
pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
查看>>
pip install mysqlclient报错
查看>>
pip install 出现报asciii码错误的解决
查看>>
pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
查看>>
pip 下载慢
查看>>
pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
查看>>
pip 安装opencv-python卡死
查看>>
pip 安装出现异常
查看>>
Pip 安装失败:需要 SSL
查看>>
Pip 安装挂起
查看>>
pip 或 pip3 为 Python 3 安装包?
查看>>
pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
查看>>
pip/pip3更换国内源
查看>>
pip3 install PyQt5 --user 失败
查看>>
pip3命令全解析:Python3包管理工具的详细使用指南
查看>>