MongoDB的索引(五)

太阳2年前技术文章1016


十一、2d Indexes


1、在MongoDB 2.2版本之前或者地址位置字段没有使用GeoJSON进行存储的情况下,我们使用2d索引比较多。

2、2d索引一般是用来计算平面上的计算,对于球面的一些几何计算,或者以GeoJSON形式来进行存储的字段,需要使用2dsphere索引

3、2d索引本质上也是一个稀疏索引

4、2d索引不支持collation选项

5、创建语法

db.<collection>.createIndex( { <location field> : "2d" ,
                               <additional field> : <value> } ,
                             { <index-specification options> } )
                             
db.collection.createIndex( { <location field> : "2d" } ,
                           { min : <lower bound> , max : <upper bound> } )      //设置最大最小边界值和精度。默认情况下,最大值和最小值的范围是[ -180 , 180 ),精度是26位的精度

6、对于2d复合索引来讲,必须将2d索引字段放在复合索引最前缀

> db.places.createIndex( { state:1,"locs": "2d"} )
{
	"ok" : 0,
	"errmsg" : "2d has to be first in index",
	"code" : 16801,
	"codeName" : "Location16801"
}

7、查询语法

1)查询在指定范围内所有的点 - 平面

语法:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $box|$polygon|$center : <coordinates>
                      } } } )

示例:

查询在[ 0 , 0 ],[ 100 , 100 ]之内的所有点:
db.places.find( { loc :
                  { $geoWithin :
                     { $box : [ [ 0 , 0 ] ,
                                [ 100 , 100 ] ]
                 } } } )
                 
查询以[-74, 40.74 ]为中心,10为半径的范围内所有的点:     
db.places.find( { loc: { $geoWithin :
                          { $center : [ [-74, 40.74 ] , 10 ]
                } } } )

2)查询球面中的范围查询

语法:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $centerSphere : [ [ <x>, <y> ] , <radius> ] }
                      } } )

示例:

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

3)查询一个平面的临近点

语法:

db.<collection>.find( { <location field> :
                         { $near : [ <x> , <y> ]
                      } } )

示例:

db.place.find( { loc :{ $near : [ 23 , 57 ]} } )

4)精确匹配一个点

语法:

db.<collection>.find( { loc: [ <x> , <y> ] } )

示例:

db.place.find( { loc : [ 23 , 57 ] } )


十二、Hash Indexes

1、hash索引可以做分片键,这会使数据分布更加随机性

2、hash索引会通过一个hash函数来计算该文档的hash索引值,hash支持嵌套文档,但是不支持多键。

3、hash索引是由MongoDB实例来自动计算使用hash索引的,应用程序无需对其进行hash计算

4、创建hash索引语法

db.collection.createIndex( { _id: "hashed" } )

5、hash索引不支持创建复合索引

6、hash索引仅支持等值查询,也可以在相同的字段创建普通索引,范围查询会优先使用普通索引,等值查询优先使用hash索引。



相关文章

yarn常用命令

1、yarn application 查看任务1.1 列出所有 Application: yarn application -list1.2 根据 Application 状态过滤:yarn appl...

压测实操--kafka-consumer压测方案

压测实操--kafka-consumer压测方案

环境信息:操作系统centos7.9,kafka版本为hdp集群中的2.0版本。Consumer相关参数使用Kafka自带的kafka-consumer-perf-test.sh脚本进行压测,该脚本参...

MySQL mgr部署文档

MySQL mgr部署文档

一、环境说明1.1服务器信息1.2目录规划1.2目录规划二、环境配置2.1 关闭防火墙和selinuxservice iptabls stop /etc/selinux/conf...

DRDS SQL闪回介绍

DRDS SQL闪回介绍

1、SQL闪回注意事项1、SQL闪回依赖RDS BINLOG保存时间,需要注意开启binlog备份。2、SQL闪回生成的恢复文件默认保存7天,闪回sql后需要尽快执行。3、SQL闪回精确匹配需要满足如...

Doris部署介绍

标准部署该文档主要介绍了部署 Doris 所需软硬件环境、建议的部署方式、集群扩容缩容,以及集群搭建到运行过程中的常见问题。在阅读本文档前,请先根据编译文档编译 Doris。软硬件需求概述Doris...

Python functools 模块

1、reduce 方法reduce 方法,顾名思义就是减少,map reduce 应用:大数据语法: reduce(function, sequence[, initial]) -> value...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。