Docs Menu

Docs HomeMongoDB Manual

reIndex

On this page

  • Definition
  • Behavior
reIndex

Tip

In mongosh, this command can also be run through the db.collection.reIndex() helper method.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

The reIndex command 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

  • reIndex may only be run on standalone instances.

  • For most users, the reIndex command is unnecessary.

Use the following syntax:

{ reIndex: <collection> }

The command takes the following fields:

Field
Description
reIndex
The name of the collection to reindex.

For MongoDB 5.0 or later, reIndex may only be run on standalone instances.

Changed in version 4.2.2.

For MongoDB 4.2.2 and later, 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, reIndex obtains a global exclusive (W) lock and blocks other operations on the mongod until it completes.

For MongoDB 3.6 and earlier, 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:

Index Builds on Populated Collections for more information on the behavior of indexing operations in MongoDB.

←  logRotaterenameCollection →

On this page

Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.