On this page本页内容
dropIndexes
The dropIndexes
command drops one or more indexes (except the index on the _id
field) from the specified collection.dropIndexes
命令从指定集合中删除一个或多个索引(_id
字段上的索引除外)。
The command has the following form:命令的格式如下:
{ dropIndexes: <string>, index: <string|document|arrayofstrings>, writeConcern: <document>, comment: <any> }
The command takes the following fields:该命令接受以下字段:
dropIndexes | String | |
index |
| |
writeConcern | document | drop command. |
comment | any |
|
Starting in MongoDB 5.2, you can use 从MongoDB 5.2开始,您可以使用dropIndexes
to drop existing indexes on the same collection even if there is a build in progress on another index. dropIndexes
删除同一集合上的现有索引,即使正在构建另一个索引。In earlier versions, attempting to drop a different index during an in-progress index build results in a 在早期版本中,在正在进行的索引生成过程中尝试删除其他索引会导致BackgroundOperationInProgressForNamespace
error.BackgroundOperationInProgressForNamespace
错误。
Starting in MongoDB 4.2, the 从MongoDB 4.2开始,dropIndexes
operation only kills queries that are using the index being dropped. dropIndexes
操作只杀死使用被删除索引的查询。This may include queries considering the index as part of query planning.这可能包括将索引作为查询规划的一部分的查询。
Prior to MongoDB 4.2, dropping an index on a collection would kill all open queries on the collection.在MongoDB 4.2之前,删除集合上的索引将杀死集合上所有打开的查询。
Changed in version 4.2.在版本4.2中更改。
dropIndexes
obtains an exclusive lock on the specified collection for the duration of the operation. 在操作期间获取指定集合的独占锁。All subsequent operations on the collection must wait until 对集合的所有后续操作必须等待dropIndexes
releases the lock.dropIndexes
释放锁。
Prior to MongoDB 4.2, 在MongoDB 4.2之前,dropIndexes
obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.dropIndexes
获得了父数据库的独占锁,在操作完成之前阻止对数据库及其所有集合的所有操作。
If the method is passed an array of index names that includes a non-existent index, the method errors without dropping any of the specified indexes.如果向该方法传递了一个包含不存在索引的索引名数组,则该方法将在不删除任何指定索引的情况下出错。
_id
You cannot drop the default index on the 不能删除_id
field._id
字段上的默认索引。
To drop a text index, specify the index name instead of the index specification document.要删除文本索引,请指定索引名称而不是索引规范文档。
Starting in MongoDB 4.4, if an index specified to 从MongoDB 4.4开始,如果指定给dropIndexes
is still building, dropIndexes
attempts to stop the in-progress build. dropIndexes
的索引仍在构建中,dropIndexes
将尝试停止正在进行的构建。Stopping an index build has the same effect as dropping the built index. 停止索引生成与删除生成的索引具有相同的效果。In versions earlier than MongoDB 4.4, 在MongoDB 4.4之前的版本中,如果集合上有任何正在进行的索引构建,dropIndexes
returns an error if there are any index builds in progress on the collection.dropIndexes
将返回错误。
For replica sets, run 对于副本集,请在primary上运行dropIndexes
on the primary. dropIndexes
。The primary stops the index build and creates an associated "abortIndexBuild" oplog entry. 主节点停止索引生成并创建关联的“abortIndexBuild”oplog条目。Secondaries which replicate the "abortIndexBuild" oplog entry stop the in-progress index build and discard the build job. 复制“abortIndexBuild”oplog项的辅助项将停止正在进行的索引生成并放弃生成作业。See Index Build Process for detailed documentation on the index build process.有关索引构建过程的详细文档,请参阅索引构建过程。
Use 使用currentOp
to identify the index builds associated with a createIndexes
or db.collection.createIndexes()
operation. currentOp
标识与createIndexes
或db.collection.createIndexes()
操作关联的索引构建。See Active Indexing Operations for an example.有关示例,请参阅活动索引操作。
Starting in version 4.4, MongoDB adds the ability to hide or unhide indexes from the query planner. 从4.4版开始,MongoDB增加了从查询计划器隐藏或取消隐藏索引的功能。By hiding an index from the planner, users can evaluate the potential impact of dropping an index without actually dropping the index.通过向计划器隐藏索引,用户可以在不实际删除索引的情况下评估删除索引的潜在影响。
If after the evaluation, the user decides to drop the index, the user can drop the hidden index; i.e. you do not need to unhide it first to drop it.如果在评估之后,用户决定删除索引,则用户可以删除隐藏索引;也就是说,您不需要先将其取消隐藏即可将其删除。
If, however, the impact is negative, the user can unhide the index instead of having to recreate a dropped index. And because indexes are fully maintained while hidden, the indexes are immediately available for use once unhidden.但是,如果影响是负面的,用户可以取消隐藏索引,而不必重新创建已删除的索引。由于索引在隐藏时得到完全维护,因此一旦取消隐藏,索引立即可用。
For more information on hidden indexes, see Hidden Indexes.有关隐藏索引的详细信息,请参阅隐藏索引。
To drop all non-要删除所有非_id
indexes , specify "*"
for the index
(See Indexes Named *
)._id
索引,请为index
指定"*"
(请参见索引名*)。
db.runCommand( { dropIndexes: "collection", index: "*" } )
To drop a single index, issue the command by specifying the name of the index you want to drop. 要删除单个索引,请通过指定要删除的索引的名称来发出命令。For example, to drop the index named 例如,要删除名为age_1
, use the following command:age_1
的索引,请使用以下命令:
db.runCommand( { dropIndexes: "collection", index: "age_1" })
mongosh
provides the helper methods 提供了助手方法db.collection.dropIndex()
and db.collection.dropIndexes()
:db.collection.dropIndex()
和db.collection.dropIndexes()
:
db.collection.dropIndex("age_1");
To drop multiple indexes, issue the command by specifying an array of the index names:要删除多个索引,请通过指定索引名称数组来发出命令:
db.runCommand( { dropIndexes: "collection", index: [ "age_1", "age_1_status_1" ] } )