MongoDB的索引(四)

太阳2年前技术文章1222

九、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的索引(五)

相关文章

Kubernetes网络模型与CNI网络插件

Kubernetes网络模型与CNI网络插件

在 Flannel 的网络插件中,容器跨主机网络的两种实现方法:UDP 和 VXLAN。它们有一个共性,就是用户的容器都连接在 docker0 网桥上。而网络插件则在宿主机上创建了一个特殊的设备(UD...

rancher证书到期处理

rancher证书到期处理

问题描述:rancher证书到期,需要更新rancher证书问题处理:基础环境信息:rancher版本: rancher:v2.4.3官方关于独立容器Rancher Server证书更新的解决方案:1...

 MySQL运维实战(1.2)安装部署:使用二进制安装部署

MySQL运维实战(1.2)安装部署:使用二进制安装部署

一般在生产环境,我们会使用二进制安装的方式安装MySQL。使用二进制安装,在处理单机多实例、升级MySQL等场景下更加方便。如果有特殊的需求(比如要打一些patch),我们还可以自己编译二进制。1、下...

大数据基础之HBase入门介绍

大数据基础之HBase入门介绍

一、HBase简介HBase – Hadoop Database,是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。HB...

MySQL运维实战之ProxySQL(9.5)proxysql和MySQL Group Replication配合使用

如果后端MySQL使用了Group Replication,可通过配置mysql_group_replication_hostgroups表来实现高可用mysql_group_replication_...

查看 Redis 不过期 key

查看 Redis 不过期 key

一、使用 Rdbtools 工具包使用 Rdbtools 工具包通过分析备份 rdb 文件,可以查看期间大 key 情况及过期时间情况。输出 csv 文档列信息情况详见下方附件--安装 rdb 工具包...

发表评论    

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