MongoDB索引(2)

1、固定集合

固定集合需要事先创建好,且大小是固定的。

如果集合已满,再向集合插入数据,则集合会自动将最老的数据删除。

固定集合不能被分片。

数据被顺序写入磁盘上的固定空间,所以写入速度非常快。

1、创建固定集合

固定集合必须在使用前显式的创建。

//"capped"为true,则为创建固定集合,创建固定级和必须指定size值,单位是字节,max是最大文档数
db.createCollection("guding_collection",{"capped":true,"size":100000,"max":100})

创建名字是”guding_collection”的大小为100000字节的固定集合。

固定集合创建好后,就不能改变了。如果需要修改固定集合的属性,只能先删除,再创建。

为固定集合指定文档数量限制时,必须同时指定固定集合的大小,不管先达到哪一个限制,之后插入的新文档都会把最老的文档挤出集合:固定集合的文档数量不能超过文档数量限制,固定集合的大小也不能超过大小限制。

将某个普通集合转换为固定集合:

//将users集合转换成固定集合
db.runCommand({"convertToCapped":"users","size":100000})

将普通集合转换为固定集合时,若原集合的大小或文档数量超过了固定集合设置的值时,会自动删除老的文档。

2、自然排序

自然排序返回集合中文档的顺序就是文档在磁盘上的顺序。

固定集合中的文档是按照文档被插入的顺序保存的,自然顺序就是文档的插入顺序。

//从旧到新排序   -1:从新到旧排序
db.users.find().sort({"$natural":1})

3、没有_id索引的集合

默认情况下,每个集合都有”id”索引。在调用createCollection创建集合时指定autoIndexId选项为false,创建集合时就不会自动在”_id”上创建索引。

“_id”索引必须是唯一索引,且一经创建就不能删除了。

Starting in MongoDB 4.0, you cannot set the option autoIndexId to false when creating collections in databases other than the localdatabase.

2、TTL索引(time-to-live index)

ttl索引允许为每一个文档设置一个过期时间,到期后自动从集合中删除。

在createIndex中指定expireAfterSeconds 选项就可以创建一个TTL索引。

只能给日期类型的字段添加ttl索引,其他字段添加ttl索引是无效的。

db.ttl.insert({"createTime":new Date()})
//给createTime添加ttl索引,并且在createTime时间15s后过期
db.ttl.createIndex({"createTime":1},{"expireAfterSeconds":15})

使用collMod命令可以修改过期时间

db.runCommand({"collMod":"ttl",index:{keyPattern:{"createTime":1},expireAfterSeconds:30}})

一个集合上可以有多个TTL索引,TTL索引是单字段索引,复合索引不支持TTL。

固定集合不能创建TTL索引。

3、全文本索引(text index)

一个集合最多只能有一个text索引。

//创建text索引
db.reviews.createIndex( { comments: "text" } )
//一个text索引可以包含多个字段
db.reviews.createIndex(
   {
     subject: "text",
     comments: "text"
   }
)

可以为每个字段指定不同的权重来控制不同字段的重要性

db.reviews.createIndex(
   {
     subject: "text",
     comments: "text"
     },
     {
     "weights":{"subject":3,"comments":2}
     }
)

使用通配符在文档的所有字段上创建text索引

db.blogs.createIndex({"$**":"text"})

对于非结构化的数据可以使用通配符来创建索引。

text索引对大小写不敏感

全文搜索

//准备数据
db.stores.insert(
   [
     { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
     { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
     { _id: 3, name: "Coffee Shop", description: "Just coffee" },
     { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
     { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
   ]
)
//创建索引
db.stores.createIndex( { name: "text", description: "text" } )

使用$text操作符来搜索

//查找包含java或coffee或clothing的文档 也可以使用逗号分隔
db.stores.find({$text:{$search:"java coffee clothing"}})
{ "_id" : 4, "name" : "Clothes Clothes Clothes", "description" : "Discount clothing" }
{ "_id" : 3, "name" : "Coffee Shop", "description" : "Just coffee" }
{ "_id" : 1, "name" : "Java Hut", "description" : "Coffee and cakes" }
{ "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }

使用”“(双引号)精确匹配

db.stores.find({$text:{$search:"\"Burger Buns\""}})
{ "_id" : 2, "name" : "Burger Buns", "description" : "Gourmet hamburgers" }

排除不需要的文档,使用”-“

db.stores.find({$text:{$search:"java shop -coffee"}})
{ "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }

可以创建一个由其他查询条件前缀和全文本字段组成的复合索引来优化全文本搜素

db.blog.createIndex({"date":1,"post":"text"})

这样对特定日期的文档使用全文查询速度就会提升很多。

猜你喜欢

转载自blog.csdn.net/wangbing25307/article/details/81004129