Database Manual / Reference / Database Commands / Administration

dropIndexes (database command数据库命令)

Definition定义

dropIndexes

Changed in version 6.0.在版本6.0中的更改。

The dropIndexes command drops one or more indexes (except the index on the _id field and the last remaining shard key index, if one exists) from the specified collection.dropIndexes命令从指定集合中删除一个或多个索引(_id字段上的索引和最后一个剩余的分片键索引除外,如果存在的话)。

Tip

In mongosh, this command can also be run through the db.collection.dropIndex() and db.collection.dropIndexes() helper methods.mongosh中,此命令也可以通过db.collection.dropIndex()db.collection.dropIndexes()辅助方法运行。.

Helper methods are convenient for mongosh users, 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.助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
dropIndexes: <string>,
index: <string|document|arrayofstrings>,
writeConcern: <document>, comment: <any>
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
dropIndexesString字符串The name of the collection whose indexes to drop.要删除其索引的集合的名称。
indexstring or document or array of strings字符串、文档或字符串数组

The index or indexes to drop.要删除的一个或多个索引。

  • To drop all indexes except the _id index and the last remaining shard key index from the collection if one exists, specify "*".要从集合中删除除_id索引和最后一个剩余的分片键索引(如果存在)之外的所有索引,请指定"*"
  • 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. 要删除单个索引,请指定索引名称、索引规范文档(除非索引是text索引)或索引名称的数组。To drop a text index, specify the index names instead of the index specification document. If this index is the last remaining shard key index, dropIndexes raises an error.要删除text索引,请指定索引名称,而不是索引规范文档。如果此索引是最后一个剩余的分片键索引,dropIndexes将引发错误。
  • To drop multiple indexes, specify an array of the index names.若要删除多个索引,请指定一个索引名称数组。
writeConcerndocument文档Optional. 可选。A document expressing the write concern of the drop command. 表示drop命令写入关注的文档。Omit to use the default write concern.省略使用默认写入关注。
commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

Behavior行为

Starting in MongoDB 6.0, dropIndexes raises an error if you attempt to use it to remove the last remaining shard key compatible index. 从MongoDB 6.0开始,如果您试图使用dropIndexes删除最后一个剩余的分片键兼容索引,则会引发错误。Passing "*" to dropIndexes drops all indexes except the _id index and the last remaining shard key compatible index, if one exists."*"传递给dropIndexes会删除除_id索引和最后一个剩余的分片键兼容索引(如果存在)之外的所有索引。

Starting in MongoDB 5.2, you can use dropIndexes to drop existing indexes on the same collection even if there is a build in progress on another index. 从MongoDB 5.2开始,即使在另一个索引上正在构建,您也可以使用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仅终止相关查询

The dropIndexes operation only kills queries that are using the index being dropped. This may include queries considering the index as part of query planning.dropIndexes操作仅终止使用被丢弃索引的查询。这可能包括将索引视为查询规划的一部分的查询。

Resource Locking资源锁定

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释放锁。

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 Indexes全文索引

To drop a text index, specify the index name instead of the index specification document.要删除文本索引,请指定索引名称,而不是索引规范文档。

Stop In-Progress Index Builds正在构建的停止索引

If an index specified to dropIndexes is still building, dropIndexes attempts to stop the in-progress build. Stopping an index build has the same effect as dropping the built index.如果指定给dropIndexes的索引仍在构建中,dropIndexes会尝试停止正在进行的构建。停止索引构建与删除已构建的索引具有相同的效果。

For replica sets, run dropIndexes on the primary. 对于副本集,在primary上运行dropIndexesThe 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标识与createIndexesdb.collection.createIndexes()操作关联的索引构建。See Active Indexing Operations for an example.有关示例,请参阅活动索引操作

Hidden Indexes隐藏索引

MongoDB offers the ability to hide or unhide indexes from the query planner. By hiding an index from the planner, you can evaluate the potential impact of dropping an index without actually dropping the index.MongoDB提供了在查询计划器中隐藏或取消隐藏索引的功能。通过对计划器隐藏索引,您可以在不实际删除索引的情况下评估删除索引的潜在影响。

If after the evaluation, the user decides to drop the index, you 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.有关隐藏索引的详细信息,请参阅隐藏索引

Examples示例

  • To drop all non-_id indexes , specify "*" for the index.要删除所有非_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" ] } )