Mongodb地理位置索引
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
db.places.ensureIndex( { loc : "2dsphere" } )
db.places.ensureIndex( { loc : "2dsphere" , category : -1, name: 1 } )
db.places.ensureIndex( { category : 1 , loc : "2dsphere" } )
单位解释:
2.查询:
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格式 |
|
2d和2dsphere支持,用于$near和$nearSphere,分别采用度和弧度 |
|
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 |
{<location field>: {$geoWithin: {$geometry: {type: <"Polygon" or "MultiPolygon"> ,coordinates: [ <coordinates> ]}}} }
{<location field>: {$geoWithin: { <shape operator>: <coordinates> }} }
{$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地理位置索引相关推荐
- mongodb地理位置索引实现原理
地理位置索引支持是MongoDB的一大亮点,这也是全球最流行的LBS服务foursquare 选择MongoDB的原因之一.我们知道,通常的数据库索引结构是B+ Tree,如何将地理位置转化为可建立B ...
- 图解GeoHash算法--MongoDB 地理位置索引的实现原理
转载自:http://blog.nosqlfan.com/html/1811.html 地理位置索引支持是MongoDB的一大亮点,这也是全球最流行的LBS服务foursquare 选择MongoDB ...
- 图解 MongoDB 地理位置索引的实现原理
地理位置索引支持是MongoDB的一大亮点,这也是全球最流行的LBS服务foursquare 选择MongoDB的原因之一.我们知道,通常的数据库索引结构是B+ Tree,如何将地理位置转化为可建立B ...
- MongoDB的地理位置索引
背景 我们平常打开一些App的时候,经常有一个"查询周边景点"的功能,如我在高德里面就查询到了附近的景点. 这种计算如果通过普通的关系型数据库,那对服务器和数据库的性能要求就太 ...
- mongodb java 地理位置_MongoDB的地理位置索引
背景 我们平常打开一些App的时候,经常有一个"查询周边景点"的功能,如我在高德里面就查询到了附近的景点. 这种计算如果通过普通的关系型数据库,那对服务器和数据库的性能要求就太高了 ...
- mongodb之索引学习
学习索引分类和创建索引: 1._id索引 大多数集合默认的索引 2.单键索引:手动创建,一个单一的值 3.多建索引:组合函数 4.复合索引 :最左前缀原则 5.过期索引 ...
- mongo 唯一约束索引_快速掌握mongoDB(三)——mongoDB的索引详解
1 mongoDB索引的管理 本节介绍mongoDB中的索引,熟悉mysql/sqlserver等关系型数据库的小伙伴应该都知道索引对优化数据查询的重要性.我们先简单了解一下索引:索引的本质就是一个排 ...
- 七(7)探花功能-MongoDB地理位置查询-附近的人
课程总结 1.探花功能 业务需求 执行过程 2.MongoDB的地理位置查询 地理位置查询的应用场景 查询案例 3.搜附近 上报地理位置 使用MongoDB搜索附近 一. 探花左划右滑 探花功能是将推 ...
- MongoDB快速上手、windows安装、常用命令、文档基本增删改查、mongoDB的索引
MongoDB快速上手 目标 理解MongoDB的业务场景.熟悉MongoDB的简介.特点和体系结构.数据类型等. 能够在Windows和Linux下安装和启动MongoDB.图形化管理界面Compa ...
最新文章
- The RSpec Book笔记《一》初步认识TDD,BDD,RSpec,Cucumber
- Andriod的Http请求获取Cookie信息并同步保存,使第二次不用登录也可查看个人信息...
- openerp 禁止导出
- 偏函数 匿名函数 高阶函数 map filter reduce
- QT5 获取窗口、系统屏幕大小尺寸信息,Qt 获取控件位置坐标,屏幕坐标,相对父窗体坐标
- 第五天、LAMP架构
- Zookeeper的ACL权限控制
- 一起来玩AZURE SQL(三)AZURE SQL 数据库迁移
- Java序列化机制和原理
- Leetcode 538.二叉树转换为累加树
- 手机版页面正式发布 html5取代wap(wml)
- python的输入和输出语句
- 在html中如何写日期的代码,日期html代码
- 【GitCracken】v8.1.1
- win10怎么更改照片分辨率和大小?图片dpi修改方法
- SpringBoot整合通用Mapper和PageHelper,使用PageHelper.startPage()失效的问题
- 【shell】【sed】在行前/行后插入一新行
- 【Web】1326- 深入浅出 Web Audio API
- 时光不负有心人 --又是折腾的一年
- 洛谷 2448 无尽的生命