dropIndexes

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:该命令接受以下字段:

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

The index or indexes to drop.

  • To drop all but the _id index from the collection, 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. 要删除单个索引,请指定索引名称、索引规范文档(除非索引是文本索引)或索引名称数组。To drop a text index, specify the index names instead of the index specification document.要删除文本索引,请指定索引名称而不是索引规范文档。
  • To drop multiple indexes (Available starting in MongoDB 4.2), specify an array of the index names.要删除多个索引(从MongoDB 4.2开始提供),请指定一个索引名称数组。
writeConcerndocumentOptional. 可选。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类型(字符串、整数、对象、数组等)。

New in version 4.4.在版本4.4中新增

Behavior行为

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仅终止相关查询

Starting in MongoDB 4.2, the dropIndexes operation only kills queries that are using the index being dropped. 从MongoDB 4.2开始,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中更改

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, dropIndexes obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.在MongoDB 4.2之前,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停止正在进行的索引生成

Starting in MongoDB 4.4, if an index specified to dropIndexes is still building, dropIndexes attempts to stop the in-progress build. 从MongoDB 4.4开始,如果指定给dropIndexes的索引仍在构建中,dropIndexes将尝试停止正在进行的构建。Stopping an index build has the same effect as dropping the built index. 停止索引生成与删除生成的索引具有相同的效果。In versions earlier than MongoDB 4.4, dropIndexes returns an error if there are any index builds in progress on the collection.在MongoDB 4.4之前的版本中,如果集合上有任何正在进行的索引构建,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隐藏的索引

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.有关隐藏索引的详细信息,请参阅隐藏索引

Examples示例

  • 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" ] } )
←  dropConnectionsfilemd5 →