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
For most users, the
db.collection.reIndex()
operationis unnecessary.Avoid running
db.collection.reIndex()
against acollection in a replica set.Do not run
db.collection.reIndex()
against acollection in a sharded cluster.
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.
- For MongoDB 2.6 through MongoDB versions withfeatureCompatibilityVersion (fCV) set to
"4.0"
or earlier, MongoDB will not create an index on a collection ifthe index entry for an existing document exceeds theMaximum Index Key Length
.
See also