定义
reIndex自版本 6.0 起已弃用。
Attempting to run the
reIndexcommand writes a warning message to the log.提示
In
mongosh, this command can also be run through thedb.collection.reIndex()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.The
reIndexcommand 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.警告
reIndexmay only be run on standalone instances.For most users, the
reIndexcommand is unnecessary.
兼容性
此命令可用于以下环境中托管的部署:
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
语法
该命令具有以下语法:
db.runCommand( { reIndex: <collection> } )
命令字段
该命令接受以下字段:
字段 | 说明 |
|---|---|
reIndex | 要重建索引的collection的名称。 |
行为
对于MongoDB5.0 或更高版本,reIndex 只能在独立运行的实例上运行。
资源锁定
reIndex 获取集合上的独占(W)锁,并阻止集合上的其他操作,直到该操作完成。
有关 MongoDB 中锁定的更多信息,请参阅常见问题解答:并发。
提示
基于填充集合的索引构建以了解有关 MongoDB 中索引操作行为的更多信息。