- _id索引
_id索引是绝大多数集合默认建立的索引,对于每个插入的数据,MongDB都会自动生成一条唯一的_id字段,db.posts.getIndexes()可以获取到该集合的索引,创建一个集合之后,系统会默认给这个集合创建一个_id索引
db.posts.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "posts.posts"
}
]
- 普通索引
创建普通索引:
db.collection.createIndex(keys, options)
语法中 Key 值为你要创建的索引字段,1 为指定按升序创建索引,如果你想按降序来创建索引指定为 -1 即可。创建索引后可以加快查询速度。
可以对文档中的多个键单独创建索引或者创建联合索引。
- MongoDB 高级索引
1)索引子文档字段:可以对文档的子文档创建索引
> db.col.insert({
... "address": {
... "city": "Los Angeles",
... "state": "California",
... "pincode": "123"
... },
... "tags": [
... "music",
... "cricket",
... "blogs"
... ],
... "name": "Tom Benzamin"
... })
WriteResult({ "nInserted" : 1 })
> db.col.ensureIndex({"address.city":1,"address.state":1,"address.pincode":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 5,
"numIndexesAfter" : 6,
"ok" : 1
}
> db.col.find({"address.city":"Los Angeles"})
{ "_id" : ObjectId("5c9c581279239019d92fa21a"), "address" : { "city" : "Los Angeles", "state" : "California", "pincode" : "123" }, "tags" : [ "music", "cricket", "blogs" ], "name" : "Tom Benzamin" }
2)数组索引
可以对文档中的数据建立索引:
> db.posts.find()
{ "_id" : ObjectId("5c9c5b8a79239019d92fa21b"), "post_text" : "enjoy the mongodb articles on Runoob", "tags" : [ "mongodb", "runoob" ] }
{ "_id" : ObjectId("5c9c5c2079239019d92fa21c"), "post_text" : "enjoy the mongodb articles on monday" }
{ "_id" : ObjectId("5c9c6f1d79239019d92fa21e"), "name" : "hi,today is sunny day,today is Monday" }
{ "_id" : ObjectId("5c9c729879239019d92fa21f"), "name" : "lihong" }
{ "_id" : ObjectId("5c9c780a79239019d92fa220"), "name" : "liming" }
> db.posts.ensureIndex({"tags":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
> db.posts.find({tags:"mongodb"})
{ "_id" : ObjectId("5c9c5b8a79239019d92fa21b"), "post_text" : "enjoy the mongodb articles on Runoob", "tags" : [ "mongodb", "runoob" ] }
- 全文检索
全文检索对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式。
开启全文检索
> db.adminCommand({setParameter:true,textSearchEnabled:true})
{
"ok" : 0,
"errmsg" : "attempted to set unrecognized parameter [textSearchEnabled], use help:true to see options "
}
创建全文索引:先在集合posts中插入两个文档,再对文档中的post_text字段建立索引,然后执行搜索
> db.posts.insert({
... "post_text": "enjoy the mongodb articles on Runoob",
... "tags": [
... "mongodb",
... "runoob"
... ]
... })
WriteResult({ "nInserted" : 1 })
> db.posts.insert({"post_text": "enjoy the mongodb articles on monday"})
WriteResult({ "nInserted" : 1 })
> db.posts.ensureIndex({post_text:"text"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.posts.find({$text:{$search:"enjoy"}})
{ "_id" : ObjectId("5c9c5b8a79239019d92fa21b"), "post_text" : "enjoy the mongodb articles on Runoob", "tags" : [ "mongodb", "runoob" ] }
{ "_id" : ObjectId("5c9c5c2079239019d92fa21c"), "post_text" : "enjoy the mongodb articles on monday" }
查找索引名:
> db.posts.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "posts.posts"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "post_text_text",
"ns" : "posts.posts",
"weights" : {
"post_text" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
删除全文检索:
db.posts.dropIndex("post_text_text")
索引是特殊的数据结构,以易于遍历的形式存储数据集的一小部分。 索引存储特定字段或一组字段的值,按照索引中指定的字段值排序。
使用索引,可以加快索引相关的查询,也相应地带来一些坏处:
每个索引占据一定的存储空间,在进行插入,更新和删除操作时也需要对索引进行操作。所以,如果你很少对集合进行读取操作,建议不使用索引。
网友评论