db.collection.reIndex()

On this page本页内容

db.collection.reIndex()

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

The db.collection.reIndex() drops all indexes on a collection and recreates them. db.collection.reIndex()删除集合上的所有索引并重新创建它们。This operation may be expensive for collections that have a large amount of data and/or a large number of indexes.对于具有大量数据和/或大量索引的集合,此操作可能代价高昂。

Warning

Behavior行为

Note

For replica sets, db.collection.reIndex() will not propagate from the primary to secondaries. 对于副本集,db.collection.reIndex()不会从primary传播到secondariesdb.collection.reIndex() will only affect a single mongod instance.db.collection.reIndex()将只影响单个mongod实例。

Resource Locking资源锁定

Changed in version 4.2.2.在版本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.对于MongoDB 4.2.2及更高版本,db.collection.reIndex()会获得集合上的独占(W)锁,并阻止对集合的其他操作,直到其完成。

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.对于MongoDB 4.0.0到4.2.1,db.collection.reIndex()会获得一个全局独占(W)锁,并阻止mongod上的其他操作,直到它完成。

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.对于MongoDB 3.6及更早版本,这些操作db.collection.reIndex()会获得数据库上的独占(W)锁,并在完成之前阻止数据库上的其他操作。

For more information on locking in MongoDB, see FAQ: Concurrency.有关MongoDB中锁定的更多信息,请参阅常见问题解答:并发

See also参阅

Indexes