博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mongodb地理位置索引
阅读量:4653 次
发布时间:2019-06-09

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

正在官方:
介绍:
建立索引:
操作器:
命令:
1.初始化集合(经度在前,纬度在后):
mongos> db.checkins.insert({ "_id" : "101", "loc" : [ 116.3447, 39.9789 ]}) mongos> db.checkins.insert({ "_id" : "102", "loc" : [ 116.3447, 39.8789 ]}) mongos> db.checkins.insert({ "_id" : "103", "loc" : [ 116.3447, 39.5789 ]})
2.创建索引 : 
mongos> db.checkins.ensureIndex({loc:"2d"})
3.查找44km以内的人
mongos> db.checkins.find({loc:{$near:[116.344722,39.9789],$maxDistance:44/111.12 }})
注意距离要除以111.2(1度=111.2km),跟平常的查找的区别仅仅是多了两个算子$near和$maxDistance
 
4.还可以利用命令geoNear来查找,还可以返回距离目标点的距离:
 db.runCommand( {    geoNear  : "places" , near : [50,50], num : 10 } );  
 
 
封装库:
 
 
2dsphere
 
在GeoJson的基础上,为了获得更加准确的运算效果,官方推荐采用2dsphere的方式
 
1.创建索引
db.places.ensureIndex( { loc : "2dsphere" } )
db.places.ensureIndex( { loc : "2dsphere" , category : -1, name: 1 } )
db.places.ensureIndex( { category : 1 , loc : "2dsphere" } )
单位解释:
a.通常经纬度是基于角度还进行衡量的,范围在-180到180之间
b.如果是2d索引,所有的查询都是基于角度的,查询的时候要把距离转成角度,即除以111.1公里
c.如果是2dsphere索引,所有的查询都是基于弧度的,查询的时候要把距离转成弧度,即除以6371公里,最后再把结果转成弧度
 
 
 
 

2.查询:

 
对于以下几个操作和命令:
 
$nearSphere/$centerSphere/$near/
geoNear command with the { spherical: true } option.
 
都是基于弧度的,在运算前和运算后要进行距离和弧度的转换
 
查询圆形范围:
db.places.find( { loc: { $geoWithin: { $centerSphere: [ [ -74, 40.74 ] ,                                                     100 / 6371 ] } } } )
查询附近点:
db.runCommand( { geoNear: "places",                 near: [ -74, 40.74 ],                 spherical: true,                 distanceMultiplier: 6371               }  )
 
不同索引对查询操作的支持情况
Name Description
2dsphere支持
2d和2dsphere支持
2d和2dsphere支持
2d和2dsphere支持
不同索引对查询子的支持情况
Name Description
$box/$center 2d支持,用于$geoWithin
$centerSphere 2d和2dsphere支持,用于$geoWithin,后者采用GeoJson格式
2d和2dsphere支持,用于$near和$nearSphere,分别采用度和弧度
2dsphere支持,用于$near和$nearSphere,采用弧度
指定查询时的GeoJson格式
指定查询时的多边形格式,用于$geoWithin,2d支持center
不同的索引的对照表
查询文档 查询的几何图形 平面类型 计算的单位 支持的索引
Returns points, lines and polygons
 
 
 
 
{ $geoWithin : {  $geometry : 
} }
polygon sphere meters 2dsphere
{ $geoIntersects : {  $geometry : 
} }
point, line or polygon sphere meters 2dsphere
{ $near : {  $geometry : 
, $maxDistance : d} }
point sphere meters

2dsphere

Returns points only
 
 
 
 
{ $geoWithin : {  $box : [[x1, y1], [x2, y2]]} }
rectangle flat flat units 2d
{ $geoWithin : {  $polygon : [[x1, y1],              [x1, y2],              [x2, y2],              [x2, y1]]} }
polygon flat flat units 2d
{ $geoWithin : {  $center : [[x1, y1], r],} }
circular region flat flat units 2d
{ $geoWithin : {  $centerSphere :    [[x, y], radius]} }
circular region sphere radians

2d

2dsphere

{ $near : [x1, y1],  $maxDistance : d}
point flat / flat units flat units

2d

a.$geoWithin
{   
: { $geoWithin: { $geometry: { type: <"Polygon" or "MultiPolygon"> , coordinates: [
] } } }}
{   
: { $geoWithin: {
:
} }}
b.$nearSphere
 
{  $nearSphere: {     $geometry: {        type : "Point",        coordinates : [ 
,
] }, $minDistance:
, $maxDistance:
}}
{  $nearSphere: [ 
,
], $minDistance:
, $maxDistance:
}

c.$near

 
{  $near: {     $geometry: {        type: "Point" ,        coordinates: [ 
,
] }, $maxDistance:
, $minDistance:
}}
{  $near: [ 
,
], $maxDistance:
}

d.$box

 
{  
: { $geoWithin: { $box: [ [
], [
] ] } }}

e.$center

 
{   
: { $geoWithin: { $center: [ [
,
] ,
] } }}

f.$centerSphere

 
{   
: { $geoWithin: { $centerSphere: [ [
,
],
] } }}

g.$polygon

 
{   
: { $geoWithin: { $polygon: [ [
,
], [
,
], [
,
], ... ] } }}
 
 
 

3.命令

 
字段 类型 描述
geoNear string 命令名.
near GeoJSON或坐标对

指定附近点的坐标

对于2dsphere用GeoJson,对于2s用坐标对

spherical Boolean

是否用球面来计算距离,如果是2dsphere必须为true

limit number 返回的最大数,默认是100
num number 同上,如果同时出现,会覆盖上面的.
minDistance number

限制的最小距离,如果是GeomJson单位为米,如果是坐标对单位为弧度

maxDistance number

 

限制的最大距离,如果是GeomJson单位为米,如果是坐标对单位为弧度

 

query document

额外的查询条件

distanceMultiplier number 对返回的基于距离的结果,乘以这个算子
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/fuland/p/4266921.html

你可能感兴趣的文章
我是怎么让全国最大的儿童失踪预警平台流量掉底的
查看>>
领扣(LeetCode)二叉树的中序遍历 个人题解
查看>>
MySQL5.5登录密码忘记了,怎嘛办?
查看>>
[javascript]9宫格拖拽拼图游戏 puzzle
查看>>
论文笔记《Hand Gesture Recognition with 3D Convolutional Neural Networks》
查看>>
java内部类
查看>>
Entity Framework底层操作封装(3)
查看>>
python 全栈开发,Day37(操作系统的发展史)
查看>>
InputStream 转换 InputStreamReader再转换BufferedReader
查看>>
在线程池中的使用spring aop事务增强
查看>>
继续深入了解Cookie 和 Session
查看>>
再看《操作系统》--处理机管理
查看>>
亚马逊的负载均衡(Amazon Load Balancing)
查看>>
Java学习之Comparable与Comparator的区别
查看>>
微信小程序canvas把正方形图片绘制成圆形
查看>>
CentOS安装python-2.7+安装pip-10.0.0
查看>>
MQTT_基础学习
查看>>
bzoj4556: [Tjoi2016&Heoi2016]字符串
查看>>
串行通信概念解析
查看>>
Sublime text 2之WIN7下安装Zencoding插件和使用
查看>>