db.collection.dropIndexes()

On this page本页内容

Definition定义

db.collection.dropIndexes()

mongo Shell Method方法

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

Drops the specified index or indexes (except the index on the _id field) 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" )

      Tip

      To 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.如果索引名数组包含不存在的索引,则该方法将在不删除任何指定索引的情况下出错。

    Tip

    To get the names of the indexes, use the db.collection.getIndexes() method.要获取索引的名称,请使用db.collection.getIndexes()方法。

The db.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, omit the parameter.要从集合中删除除_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 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), specify an array of the index names.要删除多个索引(从MongoDB 4.2开始提供),请指定一个索引名数组。

The db.collection.dropIndexes() is a wrapper around the dropIndexes command.db.collection.dropIndexes()dropIndexes命令的包装器。

Behavior行为

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. db.collection.dropIndexes()在操作期间获取指定集合的独占锁。All subsequent operations on the collection must wait until db.collection.dropIndexes() releases the lock.集合上的所有后续操作都必须等待db.collection.dropIndexes()释放锁。

Prior to 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.在MongoDB 4.2之前,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 Indexes索引

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

Aborts In-Progress Index Builds中止正在进行的索引生成

New in version 4.4:4.4版中新增:If an index specified to db.collection.dropIndexes() is still building, dropIndexes() attempts to abort the in-progress build. 如果指定给db.collection.dropIndexes()的索引仍在生成中,dropIndexes()会尝试中止正在进行的生成。Aborting an index build has the same effect as dropping the built index. 中止索引生成与删除生成的索引具有相同的效果。Prior to MongoDB 4.4, dropIndexes() would return an error if the collection had any in-progress index builds.在MongoDB 4.4之前,如果集合有任何正在生成的索引,dropIndexes()将返回错误。

For replica sets, run dropIndexes() on the primary. 对于副本集,请在primary上运行dropIndexes()The primary aborts the index build and creates an associated “abortIndexBuild” oplog entry. 主程序中止索引生成并创建关联的“abortIndexBuild”oplog条目。Secondaries which replicate the “abortIndexBuild” oplog entry abort the in-progress index build and discard the build job. 复制“abortIndexBuild”oplog条目的二级数据库将中止正在进行的索引生成并放弃生成作业。See Index Build Process for detailed documentation on the index build process and the specific timing for terminating an in-progress index build.有关索引生成过程以及终止正在进行的索引生成的具体时间安排的详细文档,请参阅索引生成过程。

The indexes specified to dropIndexes() must be the entire set of in-progress builds associated to a single createIndexes or db.collection.createIndexes() operation. 指定给dropIndexes()的索引必须是与单个createIndexesdb.collection.createIndexes()操作关联的整个正在进行的生成集。To drop a specific index out of a set of related in-progress builds, wait until the index builds complete and specify that index to dropIndexes()要从一组相关的正在进行的生成中删除特定索引,请等待索引生成完成,然后将该索引指定给dropIndexes()

For example, a createIndexes / createIndexes() operation creates three indexes. 例如,createIndexes / createIndexes()操作会创建三个索引。Assuming all three index builds are in-progress, dropIndexes() must specify all three indexes to abort the index builds and drop the indexes.假设所有三个索引生成都在进行中,dropIndexes()必须指定所有三个索引以中止索引生成并删除索引。

Use currentOp to identify the index builds associated to a createIndexes / createIndexes() operation. 使用currentOp标识与createIndexes / 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.有关隐藏索引的更多信息,请参阅隐藏索引