先給users集合插入兩條記錄,然后用users集合來進(jìn)行索引管理的演示:
成都創(chuàng)新互聯(lián)專注于海寧企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),成都商城網(wǎng)站開發(fā)。海寧網(wǎng)站建設(shè)公司,為海寧等地區(qū)提供建站服務(wù)。全流程定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)> user1={"name":"liming","age":20,"gender":"F"}
{ "name" : "liming", "age" : 20, "gender" : "F" }
> db.users.insert(user1)
WriteResult({ "nInserted" : 1 })
> user2={"name":"zhangsan","age":25,"gender":"F"}
{ "name" : "zhangsan", "age" : 25, "gender" : "F" }
> db.users.insert(user1)
WriteResult({ "nInserted" : 1 })
> db.users.count()
2
mongodb使用createIndex()和ensureIndex()方法來創(chuàng)建索引,前者用于3.0及以上版本,后者用于3.0以下版本。
語法:
db.COLLECTION_NAME.ensureIndex(keys[,options])
keys:要建立索引的參數(shù)列表。如:{KEY:1},其中key表示字段名,1表示升序排序,也可使用使用數(shù)字-1降序。
options:可選參數(shù),表示建立索引的設(shè)置??蛇x值如下:
background,Boolean,在后臺(tái)建立索引,以便建立索引時(shí)不阻止其他數(shù)據(jù)庫活動(dòng)。默認(rèn)值為false。
unique,Boolean,創(chuàng)建唯一索引。默認(rèn)值 false。
name,String,指定索引的名稱。如果未指定,MongoDB會(huì)生成一個(gè)索引字段的名稱和排序順序串聯(lián)。
partialFilterExpression, document.如果指定,MongoDB只會(huì)給滿足過濾表達(dá)式的記錄建立索引.
sparse,Boolean,對(duì)文檔中不存在的字段數(shù)據(jù)不啟用索引。默認(rèn)值是 false。
expireAfterSeconds,integer,指定索引的過期時(shí)間
storageEngine,document,允許用戶配置索引的存儲(chǔ)引擎
> db.users.createIndex({"name":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
例2:給name字段創(chuàng)建倒序索引
> db.users.createIndex({"name":-1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
例3:給name,age字段創(chuàng)建組合索引
> db.users.createIndex({"name":1,"age":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
例4:在后臺(tái)給age字段創(chuàng)建索引
> db.users.createIndex({age:1},{background:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 4,
"numIndexesAfter" : 5,
"ok" : 1
}
在后臺(tái)創(chuàng)建索引的原因:
在前臺(tái)創(chuàng)建索引期間會(huì)鎖定數(shù)據(jù)庫,會(huì)導(dǎo)致其它操作無法進(jìn)行數(shù)據(jù)讀寫,在后臺(tái)創(chuàng)建索引是,會(huì)定期釋放寫鎖,從而保證其它操作的運(yùn)行,但是后臺(tái)操作會(huì)在耗時(shí)更長(zhǎng),尤其是在頻繁進(jìn)行寫入的服務(wù)器上。
MongoDB提供的查看索引信息的方法:
getIndexes()方法可以用來查看集合的所有索引,
getIndexKeys()方法查看索引鍵。
totalIndexSize()查看集合索引的總大小,
getIndexSpecs()方法查看集合各索引的詳細(xì)信息
例1: getIndexes()的用法
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : -1
},
"name" : "name_-1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1,
"age" : 1
},
"name" : "name_1_age_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test1.users",
"background" : 1
}
]
例2:getIndexKeys()的用法
> db.users.getIndexKeys()
[
{
"_id" : 1
},
{
"name" : 1
},
{
"name" : -1
},
{
"name" : 1,
"age" : 1
},
{
"age" : 1
}
]
例3:totalIndexSize()的用法
> db.users.totalIndexSize()
81920
例4:getIndexSpecs()的用法
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : -1
},
"name" : "name_-1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1,
"age" : 1
},
"name" : "name_1_age_1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test1.users",
"background" : 1
}
]
不再需要的索引,我們可以將其刪除,mongodb提供兩種刪除索引的方法:
dropIndex()方法用于刪除指定的索引
dropIndexes()方法用于刪除全部的索引
例1:dropIndex()的用法
> db.users.dropIndex("name_1")
{ "nIndexesWas" : 5, "ok" : 1 }
> db.users.dropIndex("name_1_age_1")
{ "nIndexesWas" : 4, "ok" : 1 }
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : -1
},
"name" : "name_-1",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test1.users",
"background" : 1
}
]
我們可以看到,name字段的索引和name與age字段的組合索引皆被刪除
例2:dropIndexes()的用法
> db.users.dropIndexes()
{
"nIndexesWas" : 3,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
}
]
在使用了dropIndexes()方法后,我們之前建的所有索引都被刪除掉了
我們之前把users的索引全部刪除了,現(xiàn)在在name字段上建立一個(gè)正序索引,然后在name字段上重建倒序索引,可以看到重建索引是把之前name字段的索引刪掉再新建一個(gè)索引的,重建之前name字段還是只有一個(gè)索引.
> db.users.createIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
}
]
> db.users.reIndex({name:-1})
{
"nIndexesWas" : 2,
"nIndexes" : 2,
"indexes" : [
{
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
}
],
"ok" : 1
}
> db.users.getIndexSpecs()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test1.users"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test1.users"
}
]
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。