MongoDB的索引(五)

太阳2年前技术文章741


十一、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索引。



相关文章

Hive删除外部表

Hive删除外部表

删除外部表操作例:1.首先我们创建一个外部表create EXTERNAL table if not exists mgtest(id int, name string)row format deli...

如何卸载mysql

如何卸载mysql

1、查看安装的mysql,并停止mysqlps -ef|grep mysql #停止mysql  kill -9 pid2、卸载mysql安装...

mysql 事务隔离级别

mysql 事务隔离级别

一、事务隔离级别介绍多个连接开启各自事务操作数据库中数据时,数据库系统要负责隔离操作,以保证各个连接在获取数据时的准确性。事务隔离级别      MySQL隔离级别定义了事务与事务之间的隔离程度  二...

rancher上kube-prometheus部署报错处理

rancher上kube-prometheus部署报错处理

问题描述rancher 上安装kube-prometheus,版本:8.3.9  ,Chart 仓库:bitnami 服务 pod: prometheus-kube-prometheus-promet...

minio存储桶命名规则

存储桶命名规则创建S3存储桶后,无法更改存储桶名称,因此请明智地选择名称。重要在2018年3月1日,我们更新了美国东部(弗吉尼亚北部)地区S3存储桶的命名约定,以匹配我们在所有其他全球AWS区域中使用...

Debezium抽取SQL Server同步kafka

Debezium抽取SQL Server同步kafka

ebezium SQL Server连接器捕获SQL Server数据库模式中发生的行级更改。官方2.0文档:https://debezium.io/documentation/reference/2...

发表评论    

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