MongoDB index management--create INDEX, view index, delete index, rebuild index

Source: Internet
Author: User
Tags create index createindex

First, insert two records into the Users collection, and then use the Users collection for a demonstration of index management:

> 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
To create an index:

MongoDB uses the CreateIndex () and Ensureindex () methods to create indexes for versions 3.0 and later, which are used for versions below 3.0.
Example 1: Creating a positive-order index for the name field

> db.users.createIndex({"name":1}){    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1}

Example 2: Create a reverse index for the name field

> db.users.createIndex({"name":-1}){    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 2,    "numIndexesAfter" : 3,    "ok" : 1}

Example 3: Creating a composite index for the Name,age field

> db.users.createIndex({"name":1,"age":1}){    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 3,    "numIndexesAfter" : 4,    "ok" : 1}

Example 4: Creating an index in the background for the age field

> db.users.createIndex({age:1},{background:1}){    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 4,    "numIndexesAfter" : 5,    "ok" : 1}

Reasons for creating indexes in the background:
Locking the database during the foreground creation of the index can result in other operations being unable to read and write, creating an index in the background that periodically releases the write lock, guaranteeing the operation of other operations, but background operations can take longer, especially on servers that are frequently written.

View index:

MongoDB provides a way to view index information:
The Getindexes () method can be used to view all the indexes of a collection.
Getindexkeys () method to view the index key.
Totalindexsize () to see the total size of the collection index,
Getindexspecs () method to view the details of each index of a collection
Example of the use of 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    }]

Example of the use of 2:getindexkeys ()

> db.users.getIndexKeys()[    {        "_id" : 1    },    {        "name" : 1    },    {        "name" : -1    },    {        "name" : 1,        "age" : 1    },    {        "age" : 1    }]

Example of the use of 3:totalindexsize ()

> db.users.totalIndexSize()81920

Example of the use of 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    }]
To delete an index:

No longer need the index, we can delete it, MongoDB provides two ways to delete the index:
The Dropindex () method is used to delete the specified index
The Dropindexes () method is used to delete all indexes
Example of the use of 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    }]

We can see that the index of the name field and the combination index of the name and the age field are removed

Example of the use of 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"    }]

After using the Dropindexes () method, all of the previous indexes we built were deleted.

Index rebuild:

We have previously removed all of the users ' indexes, now establish a positive index on the name field, and then rebuild the reverse index on the name field, and you can see that the Rebuild index deletes the index of the previous name field and creates a new index, and the name field or only one index before rebuilding.

> Db.users.createIndex ({name:1}) {"createdcollectionautomatically": false, "Numindexesbefore": 1, "Numindexe        Safter ": 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}, "Nam            E ":" _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"}] 

MongoDB Index management--create INDEX, view index, delete index, rebuild index

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.