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

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

mongodb可支持空间地理搜索: 

查询器

$geoWithin       Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin. replaces $within which is deprecated.$geoIntersects     Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.$near          Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.$nearSphere       Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.

查询参数: 

Specifies a geometry in  format to geospatial query operators.
Specifies a minimum distance to limit the results of  and  queries. For use with 2dsphere index only.
Specifies a maximum distance to limit the results of  and  queries. The and  indexes support .
Specifies a circle using legacy coordinate pairs to  queries when using planar geometry. The  index supports .
Specifies a circle using either legacy coordinate pairs or  format for  queries when using spherical geometry. The  and  indexes support .
Specifies a rectangular box using legacy coordinate pairs for  queries. The  index supports .
Specifies a polygon to using legacy coordinate pairs for  queries. The  index supports.
Deprecated. Modifies a  and  queries to ensure that even if a document matches the query multiple times, the query returns the document once.

1, geoWithIn查询, 替代以前的wihin查询, 查询多边形范围内的点

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

对于大于单个半球的查询, 需要加入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" }             }          }       }     }   })

2, geoIntersects, 图形查询, 交集

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

查询大于半个半球的

db.places.find(   {     loc: {       $geoIntersects: {          $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" }             }          }       }     }   })

3, $near, 由近道原返回文档的点, 经纬度罗列方式为 [ lng, lat ]

需要创建空间索引

2dsphere index if specifying a GeoJSON point,2d index if specifying a point using legacy coordinates.
db.places.ensureIndex( { loc : "2d" } )     //应该是固定格式

 

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

使用传统坐标查询: 

db.legacy2d.find(   { location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } })

4, $nearSphere, 空间距离查询

同样需要建立空间索引

2dsphere index for location data defined as GeoJSON points2d index for location data defined as legacy coordinate pairs. To use a 2d index on GeoJSON points, create the index on the coordinates field of the GeoJSON object.

 

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

 

最大距离内查询: 

db.places.find( {   loc: { $near: [ -74 , 40 ],  $maxDistance: 10 }} )

5, $center查询, 圆形查询

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

6, $centerSphere 查询, 球星查询

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

7, $box查询, 先精度后纬度, first lower then upper

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

8, $polygon, 多边形查询

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

 

mongodb的空间位置查询

我是勤劳的搬运工: -> https://docs.mongodb.com/manual/reference/operator/query-geospatial/

转载于:https://www.cnblogs.com/wenbronk/p/7217148.html

你可能感兴趣的文章
logstash mysql 准实时同步到 elasticsearch
查看>>
Luogu2973:[USACO10HOL]赶小猪
查看>>
mabatis 中出现< 以及> 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>