db.collection.dropIndex()

On this page本页内容

Definition定义

db.collection.dropIndex(index)

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 or removes the specified index from a collection. 从集合中删除或删除指定的索引。The db.collection.dropIndex() method provides a wrapper around the dropIndexes command.db.collection.dropIndex()方法提供了dropIndexes命令的包装器。

Note

The db.collection.dropIndex() method takes the following parameter:db.collection.dropIndex()方法采用以下参数:

Parameter参数Type类型Description描述
index string or document

Optional.可选。Specifies the index to drop. 指定要删除的索引。You can specify the index either by the index name or by the index specification document.可以通过索引名称或索引规范文档指定索引。

To drop a text index, specify the index name.要删除text索引,请指定索引名称。

Starting in MongoDB 4.2, you cannot specify "*" to drop all non-_id indexes. 从MongoDB 4.2开始,不能指定"*"来删除所有非_id索引。Use db.collection.dropIndexes() instead.请改用db.collection.dropIndexes()

New in version 4.4:4.4版中新增:If an index specified to db.collection.dropIndex() is still building, dropIndex() attempts to abort the in-progress build. 如果指定给db.collection.dropIndex()的索引仍在生成中,dropIndex()将尝试中止正在进行的生成。Aborting an index build has the same effect as dropping the built index. 中止索引生成与删除生成的索引具有相同的效果。Prior to MongoDB 4.4, dropIndex() returned an error if the specified index was still building. 在MongoDB 4.4之前,如果指定的索引仍在生成,dropIndex()将返回一个错误。See Aborts In-Progress Index Builds for more complete documentation.有关更完整的文档,请参阅中止进行中的索引构建

To get the index name or the index specification document for the db.collection.dropIndex() method, use the db.collection.getIndexes() method.要获取db.collection.dropIndex()方法的索引名或索引规范文档,请使用db.collection.getIndexes()方法。

Behavior行为

Starting in MongoDB 4.2, the dropIndex() operation only kills queries that are using the index being dropped. 从MongoDB 4.2开始,dropIndex()操作只会终止使用被删除索引的查询。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.dropIndex() 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.dropIndex() releases the lock.集合上的所有后续操作都必须等待db.collection.dropIndex()释放锁。

Prior to MongoDB 4.2, db.collection.dropIndex() 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.dropIndex()在父数据库上获得了一个独占锁,阻止了对数据库及其所有集合的所有操作,直到操作完成。

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

New in version 4.4:If an index specified to db.collection.dropIndex() is still being built, dropIndex() attempts to abort the build. 如果指定给db.collection.dropIndex()的索引仍在生成中,dropIndex()会尝试中止生成。Aborting an index build has the same effect as dropping the built index. 中止索引生成与删除生成的索引具有相同的效果。Prior to MongoDB 4.4, dropIndex() returned an error if the specified index was still building.在MongoDB 4.4之前,如果指定的索引仍在生成,dropIndex()将返回一个错误。

The index specified to dropIndex() must be the only index associated to the index builder, i.e. the indexes built by a single createIndexes or db.collection.createIndexes() operation. 指定给dropIndex()的索引必须是与索引生成器关联的唯一索引,即通过单个createIndexesdb.collection.createIndexes()操作生成的索引。If the associated index builder has other in-progress builds, wait until the builds complete and specify the index to dropIndex().如果关联的索引生成器有其他正在进行的生成,请等待生成完成,并将索引指定给dropIndex()

For example, a createIndexes / createIndexes() operation creates three indexes. 例如,createIndexes/createIndexes()操作会创建三个索引。Assuming all three index builds are still in-progress, dropIndex() cannot successfully abort any of the index builds and therefore cannot drop any of those indexes.假设所有三个索引生成仍在进行中,dropIndex()无法成功中止任何索引生成,因此无法删除任何这些索引。

Use currentOp to identify the index builds associated to a createIndexes / createIndexes() operation. 使用currentOp标识与createIndexes / createIndexes()操作关联的索引生成。See Active Indexing Operations for an example.有关示例,请参阅活动索引操作

For replica sets or shard replica sets, aborting an index on the primary does not simultaneously abort secondary index builds. 对于副本集或碎片副本集,中止主索引不会同时中止辅助索引生成。dropIndex() attempts to abort the in-progress builds for the specified indexes on the primary and if successful creates an associated “abort” oplog entry. dropIndex()尝试中止primary上指定索引的正在进行的生成,如果成功,则创建关联的“中止”oplog条目。Secondary members with replicated in-progress builds wait for a commit or abort oplog entry from the primary before either committing or aborting the index build.具有复制进行中生成的Secondary成员在提交或中止索引生成之前,会等待primary的提交或中止oplog条目。

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

Example示例

Consider a pets collection. 考虑pets集合。Calling the getIndexes() method on the pets collection returns the following indexes:pets集合调用getIndexes()方法将返回以下索引:

[
   {
      "v" : 2,
      "key" : {
         "_id" : 1
      },
      "name" : "_id_"
   },
   {
      "v" : 2,
      "key" : {
         "cat" : -1
      },
      "name" : "catIdx"
   },
   {
      "v" : 2,
      "key" : {
         "cat" : 1,
         "dog" : -1
      },
      "name" : "cat_1_dog_-1"
   }
]

The single field index on the field cat has the user-specified name of catIdx [1] and the index specification document of { "cat" : -1 }.字段cat上的单字段索引具有用户指定的名称catIdx[1]和索引规范文档{ "cat" : -1 }

To drop the index catIdx, you can use either the index name:要删除索引catIdx,可以使用以下任一索引名称:

db.pets.dropIndex( "catIdx" )

Or you can use the index specification document { "cat" : -1 }:或者您可以使用索引规范文档{ "cat" : -1 }

db.pets.dropIndex( { "cat" : -1 } )
[1]During index creation, if the user does not specify an index name, the system generates the name by concatenating the index key field and value with an underscore, e.g. cat_1.在索引创建过程中,如果用户未指定索引名称,系统会通过将索引键字段和值与下划线(如cat_1)连接起来来生成名称。