db.collection.dropIndex()

Definition

  • db.collection.dropIndex(index)

mongo Shell Method

This page documents the mongo shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.

Drops or removes the specified index from a collection. Thedb.collection.dropIndex() method provides a wrapper aroundthe dropIndexes command.

Note

The db.collection.dropIndex() method takes the followingparameter:

ParameterTypeDescriptionindexstring or documentOptional. Specifies the index to drop. You can specify theindex either by the index name or by the index specificationdocument.

To drop a text index, specify theindex name.

Starting in MongoDB 4.2, you cannot specify "*" to dropall non-_id indexes. Usedb.collection.dropIndexes() instead.

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

Behavior

Starting in MongoDB 4.2, the dropIndex() operation only killsqueries that are using the index being dropped. This may includequeries considering the index as part ofquery planning.

Prior to MongoDB 4.2, dropping an index on acollection would kill all open queries on the collection.

Resource Locking

Changed in version 4.2.

db.collection.dropIndex() obtains an exclusive lock on the specified collectionfor the duration of the operation. All subsequent operations on thecollection must wait until db.collection.dropIndex() releases thelock.

Prior to MongoDB 4.2, db.collection.dropIndex() obtained an exclusivelock on the parent database, blocking all operations on thedatabase and all its collections until the operation completed.

Example

Consider a pets collection. Calling thegetIndexes() method on the pets collectionreturns the following indexes:

  1. [
  2. { "v" : 1,
  3. "key" : { "_id" : 1 },
  4. "ns" : "test.pets",
  5. "name" : "_id_"
  6. },
  7. {
  8. "v" : 1,
  9. "key" : { "cat" : -1 },
  10. "ns" : "test.pets",
  11. "name" : "catIdx"
  12. },
  13. {
  14. "v" : 1,
  15. "key" : { "cat" : 1, "dog" : -1 },
  16. "ns" : "test.pets",
  17. "name" : "cat_1_dog_-1"
  18. }
  19. ]

The single field index on the field cat has the user-specified nameof catIdx [1] and the index specification document of{ "cat" : -1 }.

To drop the index catIdx, you can use either the index name:

  1. db.pets.dropIndex( "catIdx" )

Or you can use the index specification document { "cat" : -1 }:

  1. db.pets.dropIndex( { "cat" : -1 } )
[1]During index creation, if the user does notspecify an index name, the system generates the name byconcatenating the index key field and value with an underscore,e.g. cat_1.