带驱动程序的 MongoDB
定义
db.collection.dropIndexes()从集合中删除指定的一个或多个索引(
_id字段上的索引和最后剩余的分片键索引除外)。您可以使用该方法来:
从集合中删除除
_id索引之外的所有索引。db.collection.dropIndexes() 从集合中删除指定索引。要指定索引,可以向该方法传递以下任一项目:
索引规范文档(除非索引是文本索引,在这种情况下,使用索引名称删除):
db.collection.dropIndexes( { a: 1, b: 1 } )
从集合中删除指定索引。要指定要删除的多个索引,请向该方法传递一个索引名称数组:
db.collection.dropIndexes( [ "a_1_b_1", "a_1", "a_1__id_-1" ] ) 如果索引名称数组包含不存在的索引,则该方法会出错,但不会删除任何指定索引。
提示
要获取索引的名称,请使用
db.collection.getIndexes()方法。
参数
The db.collection.dropIndexes() method takes the following optional parameter:
行为
Starting in MongoDB 6.0, db.collection.dropIndexes() raises an error if you attempt to use it to remove the last remaining shard key compatible index. Passing "*" to db.collection.dropIndexes() drops all indexes except the _id index and the last remaining shard key compatible index, if one exists.
Starting in MongoDB 5.2, you can use db.collection.dropIndexes() to drop existing indexes on the same collection even if there is a build in progress on another index. In earlier versions, attempting to drop a different index during an in-progress index build results in a BackgroundOperationInProgressForNamespace error.
仅终止相关查询
The dropIndexes() operation only kills queries that are using the index being dropped. This may include queries considering the index as part of query planning.
资源锁定
db.collection.dropIndexes() 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.dropIndexes() releases the lock.
索引名称
如果该方法传递给包含不存在索引的索引名称数组,则该方法会出错,但不会删除任何指定索引。
_id Index
不能删除 _id 字段上的默认索引。
text Indexes
要删除文本索引,请指定索引名称而不是索引规范文档。
停止进行中的索引构建
If an index specified to db.collection.dropIndexes() is still building, db.collection.dropIndexes() attempts to stop the in-progress build. Stopping an index build has the same effect as dropping the built index.
For replica sets, run db.collection.dropIndexes() on the primary. The primary stops the index build and creates an associated "abortIndexBuild" oplog entry. 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.
使用 currentOp 确定与 createIndexes 或 db.collection.createIndexes() 操作相关的索引构建。有关示例,请参阅主动索引操作。
Hidden Indexes
MongoDB 提供对查询规划器隐藏或取消隐藏索引的功能。通过向规划器隐藏索引,您可以评估在不实际删除索引的情况下删除索引的潜在影响。
如果经过评估后,用户决定删除该索引,则您可以删除隐藏索引;即不需要先取消隐藏才能删除。
如有不利影响,用户可以取消隐藏索引,而不必重新创建已删除的索引。由于索引在隐藏期间得到完全维护,因此一旦取消隐藏,索引就立即可用。
有关隐藏索引的更多信息,请参阅隐藏索引。