On this page本页内容
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
_id
field._id
字段上的默认索引。db.collection.dropIndex("*")
to drop all non-_id
indexes. db.collection.dropIndex("*")
来删除所有非id索引。db.collection.dropIndexes()
instead.db.collection.dropIndexes()
。The db.collection.dropIndex()
method takes the following parameter:db.collection.dropIndex()
方法采用以下参数:
index |
string |
|
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()
方法。
Starting in MongoDB 4.2, the 从MongoDB 4.2开始,dropIndex()
operation only kills queries that are using the index being dropped. 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之前,删除集合上的索引会终止集合上所有打开的查询。
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, 在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.db.collection.dropIndex()
在父数据库上获得了一个独占锁,阻止了对数据库及其所有集合的所有操作,直到操作完成。
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, 在MongoDB 4.4之前,如果指定的索引仍在生成,dropIndex()
returned an error if the specified index was still building.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()
的索引必须是与索引生成器关联的唯一索引,即通过单个createIndexes
或db.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条目。
Consider a 考虑pets
collection. pets
集合。Calling the 对getIndexes()
method on the pets
collection returns the following indexes:pets
集合调用getIndexes()
方法将返回以下索引:
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
,可以使用以下任一索引名称:
Or you can use the index specification document 或者您可以使用索引规范文档{ "cat" : -1 }
:{ "cat" : -1 }
:
[1] | cat_1 .cat_1 )连接起来来生成名称。 |