- _id索引
_id索引是絕大多數(shù)集合默認(rèn)建立的索引腐魂,對(duì)于每個(gè)插入的數(shù)據(jù),MongDB都會(huì)自動(dòng)生成一條唯一的_id字段理盆,db.posts.getIndexes()可以獲取到該集合的索引喊儡,創(chuàng)建一個(gè)集合之后颊糜,系統(tǒng)會(huì)默認(rèn)給這個(gè)集合創(chuàng)建一個(gè)_id索引
db.posts.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "posts.posts"
}
]
- 普通索引
創(chuàng)建普通索引:
db.collection.createIndex(keys, options)
語(yǔ)法中 Key 值為你要?jiǎng)?chuàng)建的索引字段,1 為指定按升序創(chuàng)建索引秃踩,如果你想按降序來創(chuàng)建索引指定為 -1 即可衬鱼。創(chuàng)建索引后可以加快查詢速度。
可以對(duì)文檔中的多個(gè)鍵單獨(dú)創(chuàng)建索引或者創(chuàng)建聯(lián)合索引憔杨。
- MongoDB 高級(jí)索引
1)索引子文檔字段:可以對(duì)文檔的子文檔創(chuàng)建索引
> 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)數(shù)組索引
可以對(duì)文檔中的數(shù)據(jù)建立索引:
> 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" ] }
- 全文檢索
全文檢索對(duì)每一個(gè)詞建立一個(gè)索引鸟赫,指明該詞在文章中出現(xiàn)的次數(shù)和位置,當(dāng)用戶查詢時(shí)消别,檢索程序就根據(jù)事先建立的索引進(jìn)行查找抛蚤,并將查找的結(jié)果反饋給用戶的檢索方式。
開啟全文檢索
> db.adminCommand({setParameter:true,textSearchEnabled:true})
{
"ok" : 0,
"errmsg" : "attempted to set unrecognized parameter [textSearchEnabled], use help:true to see options "
}
創(chuàng)建全文索引:先在集合posts中插入兩個(gè)文檔寻狂,再對(duì)文檔中的post_text字段建立索引霉颠,然后執(zhí)行搜索
> 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")
索引是特殊的數(shù)據(jù)結(jié)構(gòu),以易于遍歷的形式存儲(chǔ)數(shù)據(jù)集的一小部分荆虱。 索引存儲(chǔ)特定字段或一組字段的值蒿偎,按照索引中指定的字段值排序。
使用索引怀读,可以加快索引相關(guān)的查詢诉位,也相應(yīng)地帶來一些壞處:
每個(gè)索引占據(jù)一定的存儲(chǔ)空間,在進(jìn)行插入菜枷,更新和刪除操作時(shí)也需要對(duì)索引進(jìn)行操作苍糠。所以,如果你很少對(duì)集合進(jìn)行讀取操作啤誊,建議不使用索引岳瞭。