db.collection.dropIndexes()
On this page本页内容
Definition定义
db.collection.dropIndexes()
- Important
mongosh Method
This page documents a本页记录了一个mongosh
method.mongosh
方法。This is not the documentation for database commands or language-specific drivers, such as Node.js.这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the有关数据库命令,请参阅dropIndexes
command.dropIndexes
命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。For the legacy对于遗留的mongoshell文档,请参阅相应MongoDB Server版本的文档:mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Drops the specified index or indexes (except the index on the从集合中删除指定的一个或多个索引(_id
field and the last remaining shard key index) from a collection._id
字段上的索引和最后一个剩余的分片键索引除外)。You can use the method to:您可以使用该方法:Drop all but the从集合中删除除_id
index from a collection._id
索引之外的所有索引。db.collection.dropIndexes()
Drop a specified index from a collection.从集合中删除指定的索引。To specify the index, you can pass the method either:要指定索引,可以传递以下方法之一:The index specification document (unless the index is a text index in which case, use the index name to drop):索引规范文档(除非索引是text索引,否则请使用索引名称删除):db.collection.dropIndexes( { a: 1, b: 1 } )
The index name:索引名称:db.collection.dropIndexes( "a_1_b_1" )
TipTo get the names of the indexes, use the要获取索引的名称,请使用db.collection.getIndexes()
method.db.collection.getIndexes()
方法。
Drop specified indexes from a collection.从集合中删除指定的索引。(Available starting in MongoDB 4.2)(从MongoDB 4.2开始提供). To specify multiple indexes to drop, pass the method an array of index names:。若要指定要删除的多个索引,请向该方法传递一个索引名称数组:db.collection.dropIndexes( [ "a_1_b_1", "a_1", "a_1__id_-1" ] )
If the array of index names includes a non-existent index, the method errors without dropping any of the specified indexes.如果索引名称数组包含不存在的索引,则该方法将在不删除任何指定索引的情况下出错。TipTo get the names of the indexes, use the要获取索引的名称,请使用db.collection.getIndexes()
method.db.collection.getIndexes()
方法。
Thedb.collection.dropIndexes()
method takes the following optional parameter:db.collection.dropIndexes()
方法采用以下可选参数:Parameter参数Type类型Description描述indexes
string or document or array of strings字符串、文档或字符串数组Optional.可选的。Specifies the index or indexes to drop.指定要删除的索引。
To drop all but the _id index from the collection从集合中删除除_id
索引以外的所有索引, omit the parameter.,省略参数。
To drop a single index若要删除单个索引, specify either the index name, the index specification document (unless the index is a text index), or an array of the index name.,请指定索引名称、索引规范文档(除非索引是文本索引)或索引名称的数组。To drop a text index, specify the index name or an array of the index name instead of the index specification document.若要删除text索引,请指定索引名称或索引名称的数组,而不是索引规范文档。
To drop multiple indexes若要删除多个索引(Available starting in MongoDB 4.2)(从MongoDB 4.2开始提供), specify an array of the index names.,指定索引名称的数组。
Behavior行为
Starting in MongoDB 6.0, 从MongoDB 6.0开始,如果您试图使用db.collection.dropIndexes()
raises an error if you attempt to use it to remove the last remaining shard key compatible index. db.collection.dropIndexes()
来删除最后一个剩余的分片键兼容索引,则会引发错误。Passing 将"*"
to db.collection.dropIndexes()
drops all indexes except the _id
index and the last remaining shard key compatible index, if one exists."*"
传递给db.collection.dropIndexes()
将删除除_id
索引和最后一个剩余的分片键兼容索引(如果存在)之外的所有索引。
Starting in MongoDB 5.2, you can use 从MongoDB 5.2开始,您可以使用db.collection.dropIndexes()
to drop existing indexes on the same collection even if there is a build in progress on another index. db.collection.dropIndexes()
删除同一集合上的现有索引,即使在另一个索引上正在进行构建。In earlier versions, attempting to drop a different index during an in-progress index build results in a 在早期版本中,在正在进行的索引生成过程中尝试删除其他索引会导致BackgroundOperationInProgressForNamespace
error.BackgroundOperationInProgressForNamespace
错误。
Kill related queries only仅终止相关查询
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之前,删除集合上的索引会杀死该集合上所有打开的查询。
Resource Locking资源锁定
Changed in version 4.2.4.2版更改。
db.collection.dropIndexes()
obtains an exclusive lock on the specified collection for the duration of the operation. 在操作期间获取指定集合的独占锁。All subsequent operations on the collection must wait until 对集合的所有后续操作都必须等待db.collection.dropIndexes()
releases the lock.db.collection.dropIndexes()
释放锁。
Prior to MongoDB 4.2, 在MongoDB 4.2之前,db.collection.dropIndexes()
obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.db.collection.dropIndexes()
获得了对父数据库的独占锁,阻止了对数据库及其所有集合的所有操作,直到操作完成。
Index Names索引名称
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
Index
You cannot drop the default index on the 不能删除_id
field._id
字段上的默认索引。
text Indexestext索引
To drop a text index, specify the index name instead of the index specification document.若要删除text
索引,请指定索引名称,而不是索引规范文档。
Stop In-Progress Index Builds停止正在进行的索引生成
Starting in MongoDB 4.4, if an index specified to 从MongoDB 4.4开始,如果指定给db.collection.dropIndexes()
is still building, db.collection.dropIndexes()
attempts to stop the in-progress build. db.collection.dropIndexes()
的索引仍在构建中,db.collection.dropIndexes()
将尝试停止正在进行的构建。Stopping an index build has the same effect as dropping the built index. 停止索引生成与删除生成的索引具有相同的效果。In versions earlier than MongoDB 4.4, 在MongoDB 4.4之前的版本中,如果集合上有任何正在构建的索引,db.collection.dropIndexes()
returns an error if there are any index builds in progress on the collection.db.collection.dropIndexes()
将返回一个错误。
For replica sets, run 对于复制副本集,请在primary上运行db.collection.dropIndexes()
on the primary. db.collection.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”操作日志项的辅助操作会停止正在进行的索引生成并放弃生成作业。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.有关示例,请参阅活动索引操作。
Hidden Indexes隐藏索引
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.有关隐藏索引的详细信息,请参阅隐藏索引。