MongoDB dropIndex Method Example

In the following examples, we are working with:

Database: gfg

Collection: student

Document: Three documents contains name and language that students use in coding

First of all we created an index on the name field using createIndex() method:

db.student.createIndex({name:2})

Now we want to see the index name, using getIndex() so we can drop that index:

db.student.getIndexes()

Example 1: Drop the index with the name: name_1: 

db.student.dropIndex("name_1")

Here, we are going to drop the name: name_1 index using dropIndex() method. In this method, we are using the parameter as a string:

Example 2: Drop the index with the name: 2

db.student.dropIndex({name:2})

Here, we are going to drop the name: 2 index using dropIndex() method. In this method, we are using the parameter as a document:

MongoDB dropIndex() Method

MongoDB dropIndex() method drops or deletes the specified index from the given collection. It takes only one parameter which is the index, that we want to drop and it is optional.

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

Similar Reads

dropIndex Method in MongoDB

The MongoDB dropIndex() method allows for the removal of specified indexes from a collection, but it does not permit the deletion of the default index of the _id field. Additionally, hidden indexes can also be dropped using this method....

Syntax

db.Collection_Name.dropIndex(index : )...

MongoDB dropIndex Method Example

In the following examples, we are working with:...

KeyTakeAways About MongoDB dropIndex Method

The dropIndex() method in MongoDB is used to delete a specified index from a collection. It allows deletion of one index at a time, while dropIndexes() can delete multiple indexes. Starting from MongoDB 4.2, dropIndexes() only kills queries using the index being dropped. From MongoDB 4.4, dropIndex() aborts the building process of the specified index if it is still being built....

Contact Us