对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

dropIndexes(数据库命令)

dropIndexes

在版本6.0中进行了更改。

The dropIndexes command drops one or more indexes (except the index on the _id field and the last remaining shard key index, if one exists) from the specified collection.

提示

In mongosh, this command can also be run through the db.collection.dropIndex() and db.collection.dropIndexes() helper methods..

Helper methods are convenient for mongosh users, 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 对所有命令的支持的信息,请参阅不支持的命令

该命令具有以下语法:

db.runCommand(
{
dropIndexes: <string>,
index: <string|document|arrayofstrings>,
writeConcern: <document>, comment: <any>
}
)

该命令接受以下字段:

字段
类型
说明

dropIndexes

字符串

要删除索引的集合名称。

索引(index)

字符串或文档或字符串数组

要删除的一个或多个索引。

  • 要从集合中删除除 _id 索引和最后剩下的分片键索引(如果存在)之外的所有索引,请指定 "*"

  • 要删除单个索引,请指定索引名称、索引规范文档(除非该索引是文本索引)或索引名称的数组。要删除文本索引,请指定索引名称,而不是索引规范文档。如果此索引是最后剩下的分片键索引,则 dropIndexes 会引发错误。

  • 要删除多个索引,请指定索引名称的数组。

writeConcern

文档

可选。 表达 命令 写关注(write concern) drop的文档。省略以使用默认的写关注(write concern)。

comment

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 字段上的默认索引。

要删除文本索引,请指定索引名称而不是索引规范文档。

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 确定与 createIndexesdb.collection.createIndexes() 操作相关的索引构建。有关示例,请参阅主动索引操作

MongoDB 提供对查询规划器隐藏或取消隐藏索引的功能。通过向规划器隐藏索引,您可以评估在不实际删除索引的情况下删除索引的潜在影响。

如果经过评估后,用户决定删除该索引,则您可以删除隐藏索引;即不需要先取消隐藏才能删除。

如有不利影响,用户可以取消隐藏索引,而不必重新创建已删除的索引。由于索引在隐藏期间得到完全维护,因此一旦取消隐藏,索引就立即可用。

有关隐藏索引的更多信息,请参阅隐藏索引

  • 要删除所有非_id 索引,请为"*" 指定index

    db.runCommand( { dropIndexes: "collection", index: "*" } )
  • 要删除单个索引,请通过指定要删除的索引的名称来发出命令。例如,要删除名为 age_1 的索引,请使用以下命令:

    db.runCommand( { dropIndexes: "collection", index: "age_1" })

    mongosh provides the helper methods db.collection.dropIndex() and db.collection.dropIndexes():

    db.collection.dropIndex("age_1");
  • 要删除多个索引,请通过指定索引名称数组来发出命令:

    db.runCommand( { dropIndexes: "collection", index: [ "age_1", "age_1_status_1" ] } )