db.collection.reIndex()
On this page
Definition
db.collection.reIndex()
Important
mongosh Method
This is a
mongosh
method. This is not the documentation forNode.js
or other programming language specific driver methods.In most cases,
mongosh
methods work the same way as the legacymongo
shell methods. However, some legacy methods are unavailable inmongosh
.For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
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
db.collection.reIndex()
may only be run on standalone instances.For most users, the
db.collection.reIndex()
command is unnecessary.
Behavior
For MongoDB 5.0 or later, db.collection.reIndex()
may only be
run on standalone instances.
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 the Maximum Index Key Length.
Resource Locking
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.