【MongoDB學習筆記22】MongoDB的索引管理

來源:互聯網
上載者:User

標籤:mongodb   建立索引   修改索引   刪除索引   

使用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"        }    ]    >


本文出自 “緣隨心愿” 部落格,請務必保留此出處http://281816327.blog.51cto.com/907015/1600882

【MongoDB學習筆記22】MongoDB的索引管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.