reIndex
reIndex
- The
reIndex
command 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
For most users, the
reIndex
command is unnecessary.Avoid running
reIndex
for a replica set.Do not run
reIndex
against a collection in asharded cluster.
Changed in version 4.2: MongoDB disallows reIndex
to be run on amongos
, implementing a stronger restrictionagainst running reIndex
for a collection in asharded cluster.
Use the following syntax:
- { reIndex: <collection>, writeConcern: <document>}
The command takes the following fields:
FieldDescriptionreIndexThe name of the collection to reindex.writeConcernOptional. A document expressing the write concern of the drop
command.Omit to use the default write concern.
The mongo
shell provides a wrapper db.collection.reIndex()
.
Behavior
Note
For replica sets, reIndex
will not propagate from theprimary to secondaries. reIndex
willonly affect a single mongod
instance.
Important
Starting in MongoDB 4.0, reIndex
takes a globalexclusive (W) lock
and blocks other operations on thedatabase and all its collections until it finishes.
See
Index Builds on Populated Collections for more information on thebehavior of indexing operations in MongoDB.