Navigation
This version of the documentation is archived and no longer supported.

db.collection.reIndex()

On this page

db.collection.reIndex()

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

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.

db.collection.reIndex() always builds indexes in the foreground due to the logic described in Multiple Index Builds.

Changed in version 2.6: Reindexing operations will error if the index entry for an indexed field exceeds the Maximum Index Key Length. Reindexing operations occur as part of compact and repairDatabase commands as well as the db.collection.reIndex() method.

Because these operations drop all the indexes from a collection and then recreate them sequentially, the error from the Maximum Index Key Length prevents these operations from rebuilding any remaining indexes for the collection and, in the case of the repairDatabase command, from continuing with the remainder of the process.

Resource Locking

Changed in version 4.0.

Starting with MongoDB 4.0, db.collection.reIndex() obtains a global exclusive (W) lock and blocks other operations on the mongod until it completes.

For MongoDB 3.6 and earlier, these operations db.collection.reIndex() obtains an exclusive (W) lock on the database and blocks other operations on the database until finished.

For more information on locking in MongoDB, see FAQ: Concurrency.

See also

Indexes