- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.reIndex()
db.collection.reIndex()¶
On this page
-
db.collection.
reIndex
()¶ mongo
Shell MethodThis 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
For most users, the
db.collection.reIndex()
operation is unnecessary.Avoid running
db.collection.reIndex()
against a collection in a replica set.Do not run
db.collection.reIndex()
against a collection in a sharded cluster.Changed in version 4.2: MongoDB disallows
db.collection.reIndex()
to be run on amongos
, implementing a stronger restriction against runningdb.collection.reIndex()
for a collection in a sharded cluster.
Behavior¶
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.
Important
Starting in MongoDB 4.0, db.collection.reIndex()
takes a global
exclusive (W) lock
and blocks other operations on the
database and all its collections until it finishes.
- For MongoDB 2.6 through MongoDB versions with
featureCompatibilityVersion (fCV) set to
"4.0"
or earlier, MongoDB will not create an index on a collection if the index entry for an existing document exceeds theMaximum Index Key Length
.
See also