先給users集合插入兩條記錄,而後用users集合來進行索引管理的演示:mongodb
> 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()方法來建立索引,前者用於3.0及以上版本,後者用於3.0如下版本。
語法:
db.COLLECTION_NAME.ensureIndex(keys[,options])
keys:要創建索引的參數列表。如:{KEY:1},其中key表示字段名,1表示升序排序,也可以使用使用數字-1降序。
options:可選參數,表示創建索引的設置。可選值以下:
background,Boolean,在後臺創建索引,以便創建索引時不阻止其餘數據庫活動。默認值爲false。
unique,Boolean,建立惟一索引。默認值 false。
name,String,指定索引的名稱。若是未指定,MongoDB會生成一個索引字段的名稱和排序順序串聯。
partialFilterExpression, document.若是指定,MongoDB只會給知足過濾表達式的記錄創建索引.
sparse,Boolean,對文檔中不存在的字段數據不啓用索引。默認值是 false。
expireAfterSeconds,integer,指定索引的過時時間
storageEngine,document,容許用戶配置索引的存儲引擎數據庫
> db.users.createIndex({"name":1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
例2:給name字段建立倒序索引服務器
> db.users.createIndex({"name":-1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 }
例3:給name,age字段建立組合索引ide
> db.users.createIndex({"name":1,"age":1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 3, "numIndexesAfter" : 4, "ok" : 1 }
例4:在後臺給age字段建立索引spa
> db.users.createIndex({age:1},{background:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 4, "numIndexesAfter" : 5, "ok" : 1 }
在後臺建立索引的緣由:
在前臺建立索引期間會鎖定數據庫,會致使其它操做沒法進行數據讀寫,在後臺建立索引是,會按期釋放寫鎖,從而保證其它操做的運行,可是後臺操做會在耗時更長,尤爲是在頻繁進行寫入的服務器上。code
MongoDB提供的查看索引信息的方法:
getIndexes()方法能夠用來查看集合的全部索引,
getIndexKeys()方法查看索引鍵。
totalIndexSize()查看集合索引的總大小,
getIndexSpecs()方法查看集合各索引的詳細信息
例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()的用法get
> 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的索引所有刪除了,如今在name字段上創建一個正序索引,而後在name字段上重建倒序索引,能夠看到重建索引是把以前name字段的索引刪掉再新建一個索引的,重建以前name字段仍是隻有一個索引.
> 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" } ]