MongoDB – dropIndexes() Method
MongoDB is a NoSQL documentum type database. By default, _id field is an index in any collection. This indexed value uniquely identifies a document in a collection. Additionally, we can create an index for our requirement for quicker retrieval and improve the performance of a MongoDB operation. At the same time, having too many indexes also an overhead which leads to poor performance. During those times, we need to drop the indexes which are not required. So, MongoDB provides dropIndexes() method. This method drops the specified index or all index (except _id field) from the given collection.
Syntax:
To drop all the index:
db.collectionName.dropIndexes()
To drop the specified index:
db.collectionName.dropIndexes(<index>)
Here, the index parameter is an optional parameter. It specifies the indexes/index to drop.
Note: You can create index using db.collectionName.createIndex() method and to view the indexes use db.collectionName.getIndexes() method.
Important points:
- Starting from MongoDB 4.2, to specify multiple indexes to get dropped, we can use the following syntax:
db.<collection>.dropIndexes( [ “a_1_b_1”, “a_1”, “a_1__id_-1” ] )
While specifying the index names, we need to explicitly give the correct index name and if the provided index is not available(even one), then no specified indexes will be dropped.
- From MongoDB version 4.4 onwards we can use the below commands also to drop the indexes:
db.runCommand( { dropIndexes: “collectionName”, index: “<indexname1>, <indexname2>” } )
- From MongoDB 4.2, this method only kills those queries that are using the index being dropped. A lock will be established whenever this command is issued, all the operations have to wait until this dropIndexes() method call gets over.
- For dropping a text index, we should use the name of the index only.
Examples:
In the following examples, we are working with:
Database: ndtvnews
Collection: articles
Dropping _id index:
In this example we are trying to drop the _id index:
In the above image, we can see that _id and name are presented as indexes. In Mongodb, the default index is _id. It cannot be dropped. Even if we try to drop, we will get as follows:
db.articles.dropIndexes({id:1})
We have another index field named “name”. Now this is a user-defined index, so we can drop the index using dropIndexes() method.
db.articles.dropIndexes({name:1})
Here, name : 1 means that the name is the field name and provided in ascending order.
Dropping multiple indexes:
There are possibilities of more than 1 index. We can drop them easily by means of dropIndexes() method. This will drop all the indexes present in the collection except _id as it is the default index. Suppose we have multiple indexes in the articles collection.
In the above image, “_id” index is the default index, and the other two are the user-defined indexes. so, we are going to drop them using dropIndexes() method.
db.articles.dropIndexes()
Here, dropIndexes() method, drop all the indexes(except for the _id) present in the given collection.
Dropping specified indexes:
As we already know that a collection can contain multiple indexes. So, we will drop the specified index using dropIndexes() method. Suppose we have multiple indexes in the articles collection.
db.articles.dropIndexes("name_1")
Here, we drop the index using index name, i.e. name_1.
Contact Us