db.collection.dropIndex()
On this page本页内容
Definition定义
db.collection.dropIndex(index)
- Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the
dropIndexes
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Drops or removes the specified index from a collection.从集合中删除或删除指定的索引。NoteYou cannot drop the default index on the不能删除_id
field._id
字段上的默认索引。Starting in MongoDB 4.2, you cannot specify从MongoDB 4.2开始,您不能指定db.collection.dropIndex("*")
to drop all non-_id
indexes.db.collection.dropIndex("*")
来删除所有非_id
索引。Use请改用db.collection.dropIndexes()
instead.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描述index
string 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
索引,请指定索引名称。
Starting in MongoDB 4.2, you cannot specify从MongoDB 4.2开始,您不能指定"*"
to drop all non-_id
indexes."*"
来删除所有非_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,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.停止索引生成与删除生成的索引具有相同的效果。Prior to MongoDB 4.4,在MongoDB 4.4之前,如果指定的索引仍在构建中,db.collection.dropIndex()
returned an error if the specified index was still building.db.collection.dropIndex()
会返回一个错误。See Stop In-Progress Index Builds for more complete documentation.有关更完整的文档,请参阅停止正在进行的索引生成。
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资源锁定
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()
获得了对父数据库的独占锁,阻止了对数据库及其所有集合的所有操作,直到操作完成。
Stop In-Progress Index Builds停止正在进行的索引生成
Starting in MongoDB 4.4, if an index specified to 从MongoDB 4.4开始,如果指定给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. 停止索引生成与删除生成的索引具有相同的效果。In versions earlier than MongoDB 4.4, 在MongoDB 4.4之前的版本中,如果集合上有任何正在进行的索引构建,db.collection.dropIndex()
returns an error if there are any index builds in progress on the collection.db.collection.dropIndex()
将返回一个错误。
For replica sets, run 对于复制副本集,请在主服务器上运行db.collection.dropIndex()
on the primary. The primary stops the index build and creates an associated "abortIndexBuild" oplog entry. db.collection.dropIndex()
。主进程停止索引生成并创建一个关联的"abortIndexBuild"
oplog条目。Secondaries which replicate the "abortIndexBuild" oplog entry stop the in-progress index build and discard the build job. 复制"abortIndexBuild"
操作日志项的辅助操作会停止正在进行的索引生成并放弃生成作业。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. See Active Indexing Operations for an example.currentOp
标识与createIndexes
或db.collection.createIndexes()
操作关联的索引构建。有关示例,请参阅活动索引操作。
Hidden Indexes隐藏索引
Starting in version 4.4, MongoDB adds the ability to hide or unhide indexes from the query planner. By hiding an index from the planner, users can evaluate the potential impact of dropping an index without actually dropping the index.从4.4版本开始,MongoDB增加了从查询计划器隐藏或取消隐藏索引的功能。通过向计划器隐藏索引,用户可以在不实际删除索引的情况下评估删除索引的潜在影响。
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. Calling the db.collection.getIndexes()
method on the pets
collection returns the following indexes:pets
集合。对pets
集合调用db.collection.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] | cat_1 .cat_1 )连接来生成名称。 |