MongoDB的索引(四)

太阳5个月前技术文章115

九、Text Indexes

示例集合

> db.ttlsa_com.find()
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9904"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9905"), "song" : "2. Hotel California", "lyrics" : "Up ahead in the distance, I saw a shimmering light. My head grew heavy and my sight grew dim." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9906"), "song" : "3. Hotel California", "lyrics" : "Such a lovely place, Such a lovely face." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9907"), "song" : "4. Hotel California", "lyrics" : "Some dance to remember, some dance to forget." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9908"), "song" : "5. Hotel California", "lyrics" : "Welcome to the Hotel California" }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9909"), "song" : "hell world", "lyrics" : "Welcome to beijing" }

1、语法

一个集合只能有一个text索引,但是该text可以是多个字段的复合索引

db.quotes.createIndex({ content : "text" })
db.reviews.createIndex({subject: "text",comments: "text"})

2、权重

1)创建全文索引默认权重为1

2)各字段的权重分布会影响到查询时的优先策略

db.blob.insertMany([{_id: 1,content: "This morning I had a cup of coffee.",about: "beverage",keywords: ["coffee"]},
{_id: 2,content: "Who doesn't like cake?",about: "food",keywords: [ "cake", "food", "dessert" ]}])

db.blog.createIndex(
   {
     content: "text",
     keywords: "text",
     about: "text"
   },
   {
     weights: {                     //执行权重
       content: 10,
       keywords: 5
     },
     name: "TextIndex"              //指定索引名字
   }
 )

3、通配符文本索引 - 表示在所有字段创建一个全文索引

db.ttlsa_com.ensureIndex({"$**": "text"})

4、复合全文索引

> db.inventory.find()
{ "_id" : 1, "dept" : "tech", "description" : "lime green computer" }
{ "_id" : 2, "dept" : "tech", "description" : "wireless red mouse" }
{ "_id" : 3, "dept" : "kitchen", "description" : "green placemat" }
{ "_id" : 4, "dept" : "kitchen", "description" : "red peeler" }
{ "_id" : 5, "dept" : "food", "description" : "green apple" }
{ "_id" : 6, "dept" : "food", "description" : "red potato" }

> db.inventory.createIndex(
...    {
...      dept: 1,
...      description: "text"
...    }
... )

> db.inventory.find( { dept: "kitchen", $text: { $search: "green" } } )
{ "_id" : 3, "dept" : "kitchen", "description" : "green placemat" }

5、查询语法

db..find({
 $text:
   {
     $search: <string>,
     $language: <string>,
     $caseSensitive: <boolean>,
     $diacriticSensitive: <boolean>
   }
})


十、2dsphere Indexes

1、2dsphere索引支持对于球体的地理位置计算

2、2dsphere索引默认为稀疏索引,如果某一文档缺少2dsphere字段(null或为空),那么该文档不会创建索引。对于一个包含其它类型的复合2dsphere索引,该文档索引的使用仅仅与2dsphere字段有关。

> db.places.find()
{ "_id" : ObjectId("5d2fd9a67737353186206a70"), "loc" : { "type" : "Point", "coordinates" : [ -73.97, 40.77 ] }, "name" : "Central Park", "category" : "Parks" }
{ "_id" : ObjectId("5d2fd9a67737353186206a71"), "loc" : { "type" : "Point", "coordinates" : [ -73.88, 40.78 ] }, "name" : "La Guardia Airport", "category" : "Airport" }

> db.places.createIndex( { category : 1 , loc : "2dsphere" } )

> db.places.find({category:"Airport"}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "test.places",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"category" : {
				"$eq" : "Airport"
			}
		},
		"winningPlan" : {
			"stage" : "COLLSCAN",                       //全文档扫描
			"filter" : {
				"category" : {
					"$eq" : "Airport"
				}
			},
			"direction" : "forward"
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "dbslave2",
		"port" : 28002,
		"version" : "4.0.10-5",
		"gitVersion" : "7dab0a3a7b7b40cf71724b5a11eff871f8c3885c"
	},
	"ok" : 1
}

3、version 2 之后,2dsphere索引支持GeoJSON格式对象写入

4、2dsphere索引没有办法作为分片键

5、2dsphere索引字段必须是坐标或者 GeoJSON 类型,否则会报错

6、创建2dsphere索引语法:

1)2dsphere索引

db.places.createIndex( { loc : "2dsphere" } )

2)复合2dsphere索引

与2d索引不同,2dsphere索引不需要将location字段放在最左前缀。

db.places.createIndex( { category : 1 , loc : "2dsphere" } )
db.places.createIndex( { loc : "2dsphere" , category : -1, name: 1 } )

7、查询语法

Polygon相关查询

1)查询指定地址位置内所有的点

语法:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                           { $geometry :
                             { type : "Polygon" ,                   
                               coordinates : [ <coordinates> ]
                      } } } } )

示例:查询由coordinates指定的多边形内所有的点和形状

db.places.find( { loc :
                  { $geoWithin :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates : [ [
                                          [ 0 , 0 ] ,
                                          [ 3 , 6 ] ,
                                          [ 6 , 1 ] ,
                                          [ 0 , 0 ]
                                        ] ]
                } } } } )

2)交集

1.语法

db.<collection>.find( { <location field> :
                         { $geoIntersects :
                           { $geometry :
                             { type : "<GeoJSON object type>" ,
                               coordinates : [ <coordinates> ]
                      } } } } )

2.示例:查找与coordinates点组成多边形所有相交的点和形状

db.places.find( { loc :
                  { $geoIntersects :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates: [ [
                                         [ 0 , 0 ] ,
                                         [ 3 , 6 ] ,
                                         [ 6 , 1 ] ,
                                         [ 0 , 0 ]
                                       ] ]
                } } } } )

Point的点相关查询

3)临近GeoJSON Point的点

语法:

db.<collection>.find( { <location field> :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ <longitude> , <latitude> ] } ,
                             $maxDistance : <distance in meters>
                      } } } )

示例:

db.places.find( { loc :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ -88 , 30 ] } ,
                             $maxDistance : 3963
                      } } } )

4)指定point以及半径内所有的点

语法:

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

示例:

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

8、2dsphere 支持 Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon、Geometry Collection的查询


返回列表

上一篇:RBAC

下一篇:MongoDB的索引(五)

相关文章

MySQL运维实战(5.6) 字符集设置对mysqldump的影响

mysqldump不指定字符集不指定字符集时,默认使用了utf8。可能和环境有关系。mysqldump -uroot test test_load >&n...

Linux 会话管理

Linux 会话管理

在 terminal 终端中输入命令,这种用户与计算机的临时交互称为一次会话(session)。会话的一个重要特点:与其中启动的进程是连在一起的,打开窗口、会话开始,关闭窗口、会话结束,会话内部的进程...

CDH实操--客户端安装

CDH实操--客户端安装

概述安装CDH客户端,主要是方便在CDH部署节点以外,通过客户端的方式连接CDH上的hdfs,hive和hbase服务1、安装jdk(适配CDH即可,一般1.8)2、获取安装包3、部署安装包把安装包解...

TDengine集群部署

TDengine集群部署

1、基础环境操作系统:centos7.9内核版本:3.10下载地址:https://docs.taosdata.com/releases/tdengine/#3110架构设置:3 dnode,3 mn...

Linux 文本三剑客 - Grep

grep 是一个最初用于 Unix 操作系统的命令行工具。在给出文件列表或标准输入后,grep 会对匹配一个或多个正则表达式的文本进行搜索,并只输出匹配(或者不匹配)的行或文本。1970 年代,Uni...

磁盘扩容

磁盘扩容

磁盘扩容 一、linux系统1、需求由于系统盘/数据盘打满,需要扩容至100G,首先在控制台后台进行磁盘扩容。注:只能扩容磁盘的最后一个分区2、查看扩容后的磁盘空间fdisk -l3、查看磁盘分区ls...

发表评论    

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