正在官方:
介绍:http://docs.mongodb.org/manual/applications/geospatial-indexes/
建立索引:http://docs.mongodb.org/manual/tutorial/build-a-2dsphere-index/
操作器:http://docs.mongodb.org/manual/reference/operator/query-geospatial/
命令:http://docs.mongodb.org/manual/reference/command/nav-geospatial/

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 } );  
封装库:https://github.com/thephpleague/monga
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
$geoIntersects 2dsphere支持
$geoWithin 2d和2dsphere支持
$nearSphere 2d和2dsphere支持
$near 2d和2dsphere支持
不同索引对查询子的支持情况
Name Description
$box/$center 2d支持,用于$geoWithin
$centerSphere 2d和2dsphere支持,用于$geoWithin,后者采用GeoJson格式
$maxDistance 2d和2dsphere支持,用于$near和$nearSphere,分别采用度和弧度
$minDistance 2dsphere支持,用于$near和$nearSphere,采用弧度
$geometry
指定查询时的GeoJson格式
$polygon
指定查询时的多边形格式,用于$geoWithin,2d支持center

不同的索引的对照表

查询文档 查询的几何图形 平面类型 计算的单位 支持的索引
Returns points, lines and polygons
{ $geoWithin : {$geometry : <GeoJSON Polygon>
} }

polygon sphere meters 2dsphere
{ $geoIntersects : {$geometry : <GeoJSON>
} }

point, line or polygon sphere meters 2dsphere
{ $near : {$geometry : <GeoJSON Point>,$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
{<location field>: {$geoWithin: {$geometry: {type: <"Polygon" or "MultiPolygon"> ,coordinates: [ <coordinates> ]}}}
}
{<location field>: {$geoWithin: { <shape operator>: <coordinates> }}
}

b.$nearSphere
{$nearSphere: {$geometry: {type : "Point",coordinates : [ <longitude>, <latitude> ]},$minDistance: <distance in meters>,$maxDistance: <distance in meters>}
}
{$nearSphere: [ <x>, <y> ],$minDistance: <distance in radians>,$maxDistance: <distance in radians>
}

c.$near

{$near: {$geometry: {type: "Point" ,coordinates: [ <longitude> , <latitude> ]},$maxDistance: <distance in meters>,$minDistance: <distance in meters>}
}
{$near: [ <x>, <y> ],$maxDistance: <distance in radians>
}

d.$box

{<location field>: {$geoWithin: {$box: [[ <bottom left coordinates> ],[ <upper right coordinates> ]]}}
}

e.$center

{<location field>: {$geoWithin: { $center: [ [ <x>, <y> ] , <radius> ] }}
}

f.$centerSphere

{<location field>: {$geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] }}
}

g.$polygon

{<location field>: {$geoWithin: {$polygon: [ [ <x1> , <y1> ], [ <x2> , <y2> ], [ <x3> , <y3> ], ... ]}}
}

 
 
 

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

Mongodb地理位置索引相关推荐

  1. mongodb地理位置索引实现原理

    地理位置索引支持是MongoDB的一大亮点,这也是全球最流行的LBS服务foursquare 选择MongoDB的原因之一.我们知道,通常的数据库索引结构是B+ Tree,如何将地理位置转化为可建立B ...

  2. 图解GeoHash算法--MongoDB 地理位置索引的实现原理

    转载自:http://blog.nosqlfan.com/html/1811.html 地理位置索引支持是MongoDB的一大亮点,这也是全球最流行的LBS服务foursquare 选择MongoDB ...

  3. 图解 MongoDB 地理位置索引的实现原理

    地理位置索引支持是MongoDB的一大亮点,这也是全球最流行的LBS服务foursquare 选择MongoDB的原因之一.我们知道,通常的数据库索引结构是B+ Tree,如何将地理位置转化为可建立B ...

  4. MongoDB的地理位置索引

    背景 我们平常打开一些App的时候,经常有一个"查询周边景点"的功能,如我在高德里面就查询到了附近的景点. ​ 这种计算如果通过普通的关系型数据库,那对服务器和数据库的性能要求就太 ...

  5. mongodb java 地理位置_MongoDB的地理位置索引

    背景 我们平常打开一些App的时候,经常有一个"查询周边景点"的功能,如我在高德里面就查询到了附近的景点. 这种计算如果通过普通的关系型数据库,那对服务器和数据库的性能要求就太高了 ...

  6. mongodb之索引学习

    学习索引分类和创建索引:                 1._id索引 大多数集合默认的索引 2.单键索引:手动创建,一个单一的值 3.多建索引:组合函数 4.复合索引 :最左前缀原则 5.过期索引 ...

  7. mongo 唯一约束索引_快速掌握mongoDB(三)——mongoDB的索引详解

    1 mongoDB索引的管理 本节介绍mongoDB中的索引,熟悉mysql/sqlserver等关系型数据库的小伙伴应该都知道索引对优化数据查询的重要性.我们先简单了解一下索引:索引的本质就是一个排 ...

  8. 七(7)探花功能-MongoDB地理位置查询-附近的人

    课程总结 1.探花功能 业务需求 执行过程 2.MongoDB的地理位置查询 地理位置查询的应用场景 查询案例 3.搜附近 上报地理位置 使用MongoDB搜索附近 一. 探花左划右滑 探花功能是将推 ...

  9. MongoDB快速上手、windows安装、常用命令、文档基本增删改查、mongoDB的索引

    MongoDB快速上手 目标 理解MongoDB的业务场景.熟悉MongoDB的简介.特点和体系结构.数据类型等. 能够在Windows和Linux下安装和启动MongoDB.图形化管理界面Compa ...

最新文章

  1. The RSpec Book笔记《一》初步认识TDD,BDD,RSpec,Cucumber
  2. Andriod的Http请求获取Cookie信息并同步保存,使第二次不用登录也可查看个人信息...
  3. openerp 禁止导出
  4. 偏函数 匿名函数 高阶函数 map filter reduce
  5. QT5 获取窗口、系统屏幕大小尺寸信息,Qt 获取控件位置坐标,屏幕坐标,相对父窗体坐标
  6. 第五天、LAMP架构
  7. Zookeeper的ACL权限控制
  8. 一起来玩AZURE SQL(三)AZURE SQL 数据库迁移
  9. Java序列化机制和原理
  10. Leetcode 538.二叉树转换为累加树
  11. 手机版页面正式发布 html5取代wap(wml)
  12. python的输入和输出语句
  13. 在html中如何写日期的代码,日期html代码
  14. 【GitCracken】v8.1.1
  15. win10怎么更改照片分辨率和大小?图片dpi修改方法
  16. SpringBoot整合通用Mapper和PageHelper,使用PageHelper.startPage()失效的问题
  17. 【shell】【sed】在行前/行后插入一新行
  18. 【Web】1326- 深入浅出 Web Audio API
  19. 时光不负有心人 --又是折腾的一年
  20. 洛谷 2448 无尽的生命

热门文章

  1. 移动开发解决方案之玩转输入框
  2. flutter 底部动画导航栏
  3. 还是畅通工程1233
  4. Reservoir Sampling 蓄水池采样算法
  5. Putty 重新启动 linux sqlserver服务
  6. 理解React的组件
  7. 0320 关于构建之法前三章的读后感
  8. undefinednbsp;referencenbsp;to…
  9. poj 1734 (最小环)
  10. 为PHP代码在线加密