Definition定义
db.collection.dropIndex(index)Drops or removes the specified index from a collection.从集合中删除指定的索引。Note
You cannot drop the default index on the您不能删除_idfield._id字段上的默认索引。You cannot specify您不能指定db.collection.dropIndex("*")to drop all non-_idindexes. Usedb.collection.dropIndexes()instead.db.collection.dropIndex("*")来删除所有非_id索引。请改用db.collection.dropIndexes()。
To get the index name or the index specification document for the要获取db.collection.dropIndex()method, use thedb.collection.getIndexes()method.db.collection.dropIndex()方法的索引名称或索引规范文档,请使用db.collection.getIndexes()方法。Thedb.collection.dropIndex()method takes the following parameter:db.collection.dropIndex()方法接受以下参数:Parameter参数Type类型Description描述indexstring or document字符串或文档Required. 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索引,请指定索引名称。You cannot specify您不能指定"*"to drop all non-_idindexes. Usedb.collection.dropIndexes()instead."*"来删除所有非_id索引。请改用db.collection.dropIndexes()。If an index specified to如果指定给db.collection.dropIndex()is still building,db.collection.dropIndex()attempts to stop the in-progress build.db.collection.dropIndex()的索引仍在构建中,db.collection.dropIndex()将尝试停止正在进行的构建。Stopping an index build has the same effect as dropping the built index.停止索引构建与删除已构建的索引具有相同的效果。See Stop In-Progress Index Builds for more complete documentation.有关更完整的文档,请参阅停止进行中的索引构建。
Compatibility兼容性
This method 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. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关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的源代码可用、免费使用和自我管理版本
Behavior行为
Starting in MongoDB 5.2, you can use 从MongoDB 5.2开始,即使在另一个索引上正在构建,您也可以使用db.collection.dropIndex() to drop existing indexes on the same collection even if there is a build in progress on another index. db.collection.dropIndex()删除同一集合上的现有索引。In earlier versions, attempting to drop a different index during an in-progress index build results in a 在早期版本中,在正在进行的索引构建过程中尝试删除其他索引会导致BackgroundOperationInProgressForNamespace error.BackgroundOperationInProgressForNamespace错误。
Resource Locking资源锁定
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()释放锁。
Stop In-Progress Index Builds正在构建的停止索引
If an index specified to 如果指定给db.collection.dropIndex() is still building, db.collection.dropIndex() attempts to stop the in-progress build. Stopping an index build has the same effect as dropping the built index.db.collection.dropIndex()的索引仍在构建中,db.collection.dropIndex()将尝试停止正在进行的构建。停止索引构建与删除已构建的索引具有相同的效果。
For replica sets, run 对于副本集,在primary副本上运行db.collection.dropIndex() on the primary. db.collection.dropIndex()。The 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. See Index Build Process for detailed documentation on the index build process.复制"abortIndexBuild"oplog条目的辅助人员会停止正在进行的索引构建并丢弃构建作业。有关索引构建过程的详细文档,请参阅索引构建过程。
Use 使用currentOp to identify the index builds associated with a createIndexes or db.collection.createIndexes() operation. currentOp标识与createIndexes或db.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.有关隐藏索引的详细信息,请参阅隐藏索引。
Example示例
In a 在pets collection, create a descending index on the cat field:pets集合中,在cat字段上创建一个降序索引:
db.pets.createIndex( { cat: -1 }, { name: "catIdx" } )
To see the indexes in the 要查看pets collection, run the getIndexes() method:pets集合中的索引,请运行getIndexes()方法:
db.pets.getIndexes()
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { cat: -1 }, name: 'catIdx' }
]
The single field index on the field 字段cat has the user-specified name of catIdx and the index specification document of { "cat" : -1 }.cat上的单个字段索引具有用户指定的名称catIdx和索引规范文档{ "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 } )
The dropIndex command returns the number of indexes in the collection prior to the command being run, and indicates whether the command was successful:dropIndex命令返回运行该命令之前集合中的索引数,并指示该命令是否成功:
{ nIndexesWas: 2, ok: 1 }
To confirm that the index was dropped, run the 要确认索引已删除,请再次运行getIndexes() method again:getIndexes()方法:
db.pets.getIndexes()
[ { v: 2, key: { _id: 1 }, name: '_id_' } ]