您好,登錄后才能下訂單哦!
使用ensureIndex函數創建集合的索引。
對集合而言,每個索引只需要創建一次,重復創建沒有任何作用;
> show collections system.indexes users
所有的數據庫的索引元數據都存放在system.indexs的集合中;
使用getIndexes函數查看給定的集合上的索引信息:
> db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "blog.users" }, { "v" : 1, "key" : { "username" : 1 }, "name" : "username_1", "ns" : "blog.users" }, { "v" : 1, "key" : { "age" : 1, "username" : 1 }, "name" : "age_1_username_1", "ns" : "blog.users" } ] >
可以適應dropIndex函數刪除指定的索引:
> db.users.dropIndex({"username":1}) { "nIndexesWas" : 3, "ok" : 1 } > db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "blog.users" }, { "v" : 1, "key" : { "age" : 1, "username" : 1 }, "name" : "age_1_username_1", "ns" : "blog.users" } ] >
上述的例子中可以看到索引的標識默認格式為keyname1_dir1_keyname2_dir….keynameN_DirN;
也可以指定標識名:
> db.users.ensureIndex({"username":1},{"name":"firstname"}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 } > db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "blog.users" }, { "v" : 1, "key" : { "age" : 1, "username" : 1 }, "name" : "age_1_username_1", "ns" : "blog.users" }, { "v" : 1, "key" : { "username" : 1 }, "name" : "firstname", "ns" : "blog.users" } ] >
修改索引的步驟:刪除索引—>創建索引;
刪除索引可以直接指定索引標識,例如:
> db.users.dropIndex("firstname") { "nIndexesWas" : 3, "ok" : 1 } > db.users.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "blog.users" }, { "v" : 1, "key" : { "age" : 1, "username" : 1 }, "name" : "age_1_username_1", "ns" : "blog.users" } ] >
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。