博客
关于我
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/

你可能感兴趣的文章
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs字符与字节之间的转换
查看>>
NodeJs学习笔记001--npm换源
查看>>
NodeJs学习笔记002--npm常用命令详解
查看>>
nodejs学习笔记一——nodejs安装
查看>>
nodejs封装http请求
查看>>
nodejs常用组件
查看>>
nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
查看>>
Nodejs异步回调的处理方法总结
查看>>
NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
查看>>
Nodejs教程09:实现一个带接口请求的简单服务器
查看>>
nodejs服务端实现post请求
查看>>
nodejs框架,原理,组件,核心,跟npm和vue的关系
查看>>
Nodejs模块、自定义模块、CommonJs的概念和使用
查看>>
nodejs生成多层目录和生成文件的通用方法
查看>>
nodejs端口被占用原因及解决方案
查看>>
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
nodejs系列之Koa2
查看>>