博客
关于我
mongodb-地理坐标存储查询
阅读量:801 次
发布时间: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/

你可能感兴趣的文章
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>
npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
查看>>
npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
查看>>
npm—小记
查看>>
npm介绍以及常用命令
查看>>
NPM使用前设置和升级
查看>>
npm入门,这篇就够了
查看>>
npm切换到淘宝源
查看>>
npm切换源淘宝源的两种方法
查看>>
npm前端包管理工具简介---npm工作笔记001
查看>>
npm包管理深度探索:从基础到进阶全面教程!
查看>>
npm升级以及使用淘宝npm镜像
查看>>
npm发布包--所遇到的问题
查看>>
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和package.json那些不为常人所知的小秘密
查看>>