- Reference >
mongosh
Methods >- Collection Methods >
- db.collection.dropIndex()
db.collection.dropIndex()¶
On this page
Definition¶
-
db.collection.
dropIndex
(index)¶ Important
mongosh
MethodThis page documents a
mongosh
method. This is not the documentation for a language-specific driver such as Node.js.For MongoDB API drivers, refer to the language-specific :driver:`MongoDB driver documentation </>`.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Drops or removes the specified index from a collection. The
db.collection.dropIndex()
method provides a wrapper around thedropIndexes
command.Note
- You cannot drop the default index on the
_id
field. - Starting in MongoDB 4.2, you cannot specify
db.collection.dropIndex("*")
to drop all non-_id
indexes. Usedb.collection.dropIndexes()
instead.
The
db.collection.dropIndex()
method takes the following parameter:Parameter Type Description index
string or document Optional. Specifies the index to drop. You can specify the index either by the index name or by the index specification document.
To drop a text index, specify the index name.
Starting in MongoDB 4.2, you cannot specify
"*"
to drop all non-_id
indexes. Usedb.collection.dropIndexes()
instead.New in version 4.4: If an index specified to
db.collection.dropIndex()
is still building,db.collection.dropIndex()
attempts to stop the in-progress build. Stopping an index build has the same effect as dropping the built index. Prior to MongoDB 4.4,db.collection.dropIndex()
returned an error if the specified index was still building. See Stop In-Progress Index Builds for more complete documentation.To get the index name or the index specification document for the
db.collection.dropIndex()
method, use thedb.collection.getIndexes()
method.- You cannot drop the default index on the
Behavior¶
Starting in MongoDB 5.2, you can use db.collection.dropIndex()
to drop existing
indexes on the same collection even if there is a build in progress on
another index. In earlier versions, attempting to drop a different
index during an in-progress index build results in a
BackgroundOperationInProgressForNamespace
error.
Resource Locking¶
Changed in version 4.2.
db.collection.dropIndex()
obtains an exclusive lock on the specified collection
for the duration of the operation. All subsequent operations on the
collection must wait until db.collection.dropIndex()
releases the
lock.
Prior to MongoDB 4.2, db.collection.dropIndex()
obtained an exclusive
lock on the parent database, blocking all operations on the
database and all its collections until the operation completed.
Stop In-Progress Index Builds¶
Starting in MongoDB 4.4, if an index specified to db.collection.dropIndex()
is still
building, db.collection.dropIndex()
attempts to stop the in-progress build. Stopping
an index build has the same effect as dropping the built index. In
versions earlier than MongoDB 4.4, db.collection.dropIndex()
returns an error if
there are any index builds in progress on the collection.
For replica sets, run db.collection.dropIndex()
on the primary.
The primary stops the index build and creates an associated
“abortIndexBuild” oplog entry. Secondaries which replicate
the “abortIndexBuild” oplog entry stop the in-progress index build and
discard the build job. See Index Build Process for detailed
documentation on the index build process.
Use currentOp
to identify the index builds associated with
a createIndexes
or db.collection.createIndexes()
operation. See Active Indexing Operations for an example.
Example¶
Consider a pets
collection. Calling the
db.collection.getIndexes()
method on the pets
collection
returns the following indexes:
The single field index on the field cat
has the user-specified name
of catIdx
[1] and the index specification document of
{ "cat" : -1 }
.
To drop the index catIdx
, you can use either the index name:
Or you can use the index specification document { "cat" : -1 }
:
[1] | During index creation, if the user does not
specify an index name, the system generates the name by
concatenating the index key field and value with an underscore,
e.g. cat_1 . |