> db.table1.ensureIndex({name:1},{backgroud:true}) #1表明升序,-1降序,backgroud默認false,會阻塞其餘數據庫操做數據庫
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
> db.table1.ensureIndex({name:1,age:-1},{backgroud:true}) #多列索引code
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 }
> db.table1.getIndexKeys()索引
[ { "_id" : 1 }, { "name" : 1 }, { "name" : 1, "age" : -1 } ] #_id默認就有
####刪除索引get
> db.table1.dropIndex({name:1}) #刪除name:1的索引io
{ "nIndexesWas" : 3, "ok" : 1 }
> db.table1.dropIndexes() #刪除全部除_id的索引table
{ "nIndexesWas" : 2, "msg" : "non-_id indexes dropped for collection", "ok" : 1 }
讀取索引 > db.table1.reIndex({name:1,age:-1},{backgroud:true})call
{ "nIndexesWas" : 2, "nIndexes" : 2, "indexes" : [ { "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "db1.table1" }, { "key" : { "name" : 1, "age" : -1 }, "name" : "name_1_age_-1", "ns" : "db1.table1", "backgroud" : true } ], "ok" : 1 }