db.collection.reIndex()

  • db.collection.reIndex()

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.

The db.collection.reIndex() drops all indexes on acollection and recreates them. This operation may be expensive forcollections that have a large amount of data and/or a large numberof indexes.

Warning

Changed in version 4.2: MongoDB disallows db.collection.reIndex() to berun on a mongos, implementing a strongerrestriction against runningdb.collection.reIndex() for a collection in asharded cluster.

Behavior

Note

For replica sets, db.collection.reIndex() will not propagate from theprimary to secondaries. db.collection.reIndex() willonly affect a single mongod instance.

Important

Starting in MongoDB 4.0, db.collection.reIndex() takes a globalexclusive (W) lock and blocks other operations on thedatabase and all its collections until it finishes.

See also

Indexes