db.collection.reIndex()
On this page
db.collection.reIndex()
Important
mongo Shell Method
This page documents a
mongo
method. This is not the documentation for database commands or language-specific drivers, such as Node.js. To use the database command, see thereIndex
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
The
db.collection.reIndex()
drops all indexes on a collection and recreates them. This operation may be expensive for collections that have a large amount of data and/or a large number of indexes.Warning
For most users, the
db.collection.reIndex()
operation is unnecessary.Avoid running
db.collection.reIndex()
against a collection in a replica set.Do not run
db.collection.reIndex()
against a collection in a sharded cluster.Changed in version 4.2: MongoDB disallows
db.collection.reIndex()
to be run on amongos
, implementing a stronger restriction against runningdb.collection.reIndex()
for a collection in a sharded cluster.
Behavior
Note
For replica sets, db.collection.reIndex()
will not propagate from the
primary to secondaries. db.collection.reIndex()
will
only affect a single mongod
instance.
Resource Locking
Changed in version 4.2.2.
For MongoDB 4.2.2 and later, db.collection.reIndex()
obtains
an exclusive (W) lock on the collection and blocks other operations on
the collection until it completes.
For more information on locking in MongoDB, see FAQ: Concurrency.