db.collection.reIndex()

On this page本页内容

Definition定义

db.collection.reIndex()
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

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. 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行为

For MongoDB 5.0 or later, db.collection.reIndex() may only be run on standalone instances.对于MongoDB 5.0或更高版本,db.collection.reIndex()只能在单机版实例上运行。

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.对于featureCompatibilityVersion(fCV)设置为“4.0”或更早版本的MongoDB 2.6到MongoDB版本,如果现有文档的索引条目超过最大索引密钥长度,MongoDB将不会在集合上创建索引。

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中锁定的更多信息,请参阅常见问题解答:并发

Tip提示
See also: 参阅:
←  db.collection.mapReduce()db.collection.remove() →