定义
dropIndexes在版本6.0中进行了更改。
The
dropIndexescommand drops one or more indexes (except the index on the_idfield and the last remaining shard key index, if one exists) from the specified collection.提示
In
mongosh, this command can also be run through thedb.collection.dropIndex()anddb.collection.dropIndexes()helper methods..Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { dropIndexes: <string>, index: <string|document|arrayofstrings>, writeConcern: <document>, comment: <any> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
dropIndexes | 字符串 | 要删除索引的集合名称。 |
索引(index) | 字符串或文档或字符串数组 | |
writeConcern | 文档 | 可选。 表达 命令 写关注(write concern) |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 |
行为
从MongoDB6.0 开始,如果您尝试使用dropIndexes 删除最后一个剩余的分片键兼容索引,则会引发错误。将"*" 传递给dropIndexes 会删除除_id 索引和最后剩下的分片键兼容索引(如果存在)之外的所有索引。
从MongoDB5.2 开始,即使正在构建另一个索引,也可以使用dropIndexes 删除同一集合上的现有索引。在早期版本中,在索引构建期间尝试删除其他索引会导致BackgroundOperationInProgressForNamespace 错误。
仅终止相关查询
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.
资源锁定
dropIndexes obtains an exclusive lock on the specified collection for the duration of the operation. All subsequent operations on the collection must wait until dropIndexes releases the lock.
索引名称
如果该方法传递给包含不存在索引的索引名称数组,则该方法会出错,但不会删除任何指定索引。
_id Index
不能删除 _id 字段上的默认索引。
text Indexes
要删除文本索引,请指定索引名称而不是索引规范文档。
停止进行中的索引构建
If an index specified to dropIndexes is still building, 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 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 提供对查询规划器隐藏或取消隐藏索引的功能。通过向规划器隐藏索引,您可以评估在不实际删除索引的情况下删除索引的潜在影响。
如果经过评估后,用户决定删除该索引,则您可以删除隐藏索引;即不需要先取消隐藏才能删除。
如有不利影响,用户可以取消隐藏索引,而不必重新创建已删除的索引。由于索引在隐藏期间得到完全维护,因此一旦取消隐藏,索引就立即可用。
有关隐藏索引的更多信息,请参阅隐藏索引。
示例
要删除所有非
_id索引,请为"*"指定index。db.runCommand( { dropIndexes: "collection", index: "*" } ) 要删除单个索引,请通过指定要删除的索引的名称来发出命令。例如,要删除名为
age_1的索引,请使用以下命令:db.runCommand( { dropIndexes: "collection", index: "age_1" }) mongoshprovides the helper methodsdb.collection.dropIndex()anddb.collection.dropIndexes():db.collection.dropIndex("age_1"); 要删除多个索引,请通过指定索引名称数组来发出命令:
db.runCommand( { dropIndexes: "collection", index: [ "age_1", "age_1_status_1" ] } )