Viewing and Droping the indexes of a collection in MongoDB


>_ How to view the indexes created for a collection?

MongoDB provides three methods for viewing indexes of a document those are

  • getIndexes() -- will display all the indexes with details like details like version, name, unique or not etc.
  • getIndexKeys() -- displays only index keys (field for which indexing done)
  • getIndexSpecs() -- same as getIndexes()

>_ Syntaxes

db.collectioname.getIndexes()
db.collectioname.getIndexKeys()
db.collectioname.getIndexSpecs()

Example:-

>_ View the indexes of 'users' collection

>db.users.getIndexes()

Output:-

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "TutorialToUs.users"
    },
    {
        "v" : 1,
        "unique" : true,
        "key" : {
            "userid" : 1.0
        },
        "name" : "UniqueNameIndex",
        "ns" : "TutorialToUs.users"
    },
    {
        "v" : 1,
        "key" : {
            "Age" : 1.0
        },
        "name" : "Age_1",
        "ns" : "TutorialToUs.users"
    }, 
    {
        "v" : 1,
        "unique" : true,
        "key" : {
            "firsname" : 1.0,
            "lastname" : 1.0
        },
        "name" : "compundindexfirsnamelastname",
        "ns" : "TutorialToUs.users"
    }
]

>_ List out the 'Index Keys' of 'user' collection

>db.users.getIndexKeys()

Output:-

[
    {
        "_id" : 1
    },
    {
        "firsname" : 1.0,
        "lastname" : 1.0
    },
    {
        "userid" : 1.0
    },
    {
        "Age" : 1.0
    }
]
              

>_ Droping Index

  1. We can use dropIndex(indexname) method to remove a specific index in a collection
  2. use dropIndexes() to remove all Indexes of a collection(except _id index) .

Synatxes

  • db.collection.dropIndex(index)
  • db.collection.dropIndexes()
Parameter Type Description
index string or document Specifies the index to drop. You can specify the index either by the index name or by the index specification document

To get the index name or the index specification document for the db.collection.dropIndex() method, use the db.collection.getIndexes() method.


Example:-

>_ Drop the Index created earlier on 'Age' field for the 'users' collection.

db.users.dropIndex("Age_1")

or

db.users.dropIndex({Age:1})

>_ Drop all indexes 'users' collection.

db.users.dropIndexes()

>_ Note:-

  • We Can't drop the index created for "_id" field.
  • To add or change index options you must drop the index using the dropIndex() method and issue another createIndex() operation with the new options.
  • If you create an index with one set of options, and then issue the createIndex() method with the same index fields and different options without first dropping the index, createIndex() will not rebuild the existing index with the new options.
  • If you call multiple createIndex() methods with the same index specification at the same time, only the first operation will succeed, all other operations will have no effect.
  • Non-background indexing operations will block all other operations on a database.
Copyright © 2018-2020 TutorialToUs. All rights reserved.