Docs Menu

db.collection.reIndex()

On this page

  • Behavior
db.collection.reIndex()
Important
mongo Shell Method

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

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
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.

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 MongoDB 4.0.0 through 4.2.1, 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.

Tip
See also:

On this page

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.