MongoDB dropIndex() Method (original) (raw)
Last Updated : 05 Feb, 2025
Indexes are important in MongoDB for improving query performance, allowing the database to quickly find the documents that match query criteria. The dropIndex()
method in MongoDB enables developers to manage their **collection’s indexes by removing unnecessary or outdated indexes. However, it’s important to note that we cannot drop the default index on the **_id
**field using this method.
In this article, we’ll explain the dropIndex()
method in detail, its **syntax, **usage, **examples, and important considerations to help us manage indexes in MongoDB efficiently
What is the dropIndex() Method in MongoDB?
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. Starting from MongoDB 4.4, if the specified index is still being built, the dropIndex() method will **abort the building process of the index, providing developers with greater control over **index management in MongoDB collections.
Key points about the dropIndex()
method:
- It removes a **single index at a time.
- It cannot remove the default
_id
index, as MongoDB automatically creates and requires this index. - Starting from MongoDB **4.4, if an index is still being built,
dropIndex()
can abort the building process. - Starting from MongoDB **4.2, we cannot drop all non-
_id
indexes using thedb.Collection_Name.dropIndex("*")
method. Instead, use thedropIndexes()
method to remove multiple indexes.
**Syntax:
db.Collection_Name.dropIndex(index : <document/string>)
**Key Terms
- **collection_name: The name of the collection whose index we want to drop.
- **index: The index to drop. This can be specified either as a string (index name) or as a document (index specification)
Return Value of dropIndex()
The dropIndex()
method returns a document with the following fields:
- **nIndexesWas: The number of indexes before the index was dropped.
- **ok: A value of
1
indicating the operation was successful.
**Example of Using MongoDB dropIndex() Method
Let’s walk through a few practical examples using MongoDB’s dropIndex()
method. In these examples, we are working with:
- **Database: gfg
- **Collection: student
- **Document: Three documents contains name and language that students use in coding
Sample Documents in the student
Collection:
Create an Index on the name
Field
First of all we created an index on the name field using **createIndex() method:
db.student.createIndex({name:2})
This creates an ascending index on the name
field. Now, let’s check the created index using the getIndexes()
method.
db.student.getIndexes()
**Output:
**Explanation:
- The first index is the default
_id
index. - The second index is the new
name_1
index we just created on thename
field.
**Example 1: Drop the Index Using the Index Name
To remove the index created on the **name
**field, we can use the **index name (name_1
) with the dropIndex()
method.
**Query:
db.student.dropIndex("name_1")
**Output:
**Explanation:
- The index named
name_1
on thename
field is successfully dropped. nIndexesWas
shows that the number of indexes before dropping the index was 2, andok: 1
indicates the operation was successful.
**Example 2: Drop the Index Using the Index Specification Document
We can also drop the index using the **index specification document instead of the index name.
1. First, create an index on the name
field with descending order.
db.student.createIndex({ name: -1 })
2. Now, drop the index by specifying the index document.
db.student.dropIndex({ name: -1 })
**Output:
**Explanation:
- The index on the
name
field with descending order (name: -1
) is successfully dropped using the index specification document. - As before,
nIndexesWas
shows the number of indexes before the operation (2), andok: 1
confirms the success of the operation.
Key TakeAways About MongoDB dropIndex()
- **Single Index Deletion: The
dropIndex()
method removes one index at a time. To drop multiple indexes, use thedropIndexes()
method. - **Cannot Drop
_id
Index: MongoDB does not allow dropping the default_id
index, which is essential for document identification. - **Hidden Indexes: Hidden indexes, which are not used in query execution by default, can also be dropped using this method.
- **Abort Index Building (MongoDB 4.4+): If an index is still being built,
dropIndex()
can be used to abort the process. dropIndexes()
for All Non-_id
Indexes (MongoDB 4.2+): From MongoDB 4.2 onwards, you cannot usedropIndex("*")
to drop all non-_id
indexes. Instead, usedropIndexes()
to drop multiple indexes.
Conclusion
The dropIndex()
method in MongoDB is a vital tool for index management, allowing developers to remove unnecessary or outdated indexes, optimize database performance, and free up storage. Understanding when and how to use dropIndex()
efficiently is key to managing a MongoDB database. From simple index deletions to handling hidden indexes and aborted index builds (starting in MongoDB 4.4), this method provides essential functionality for maintaining your MongoDB collections.