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

db. 集合.dropIndexes()(mongosh方法)

带驱动程序的 MongoDB

此页面记录了mongosh方法。要查看MongoDB驾驶员中的等效方法,请参阅您的编程语言的相应页面:
db.collection.dropIndexes()

从集合中删除指定的一个或多个索引(_id 字段上的索引和最后剩余的分片键索引除外)。

您可以使用该方法来:

  • 从集合中删除除 _id 索引之外的所有索引。

    db.collection.dropIndexes()
  • 从集合中删除指定索引。要指定索引,可以向该方法传递以下任一项目:

    • 索引规范文档(除非索引是文本索引,在这种情况下,使用索引名称删除):

      db.collection.dropIndexes( { a: 1, b: 1 } )
    • 索引名称:

      db.collection.dropIndexes( "a_1_b_1" )

      提示

      要获取索引的名称,请使用 db.collection.getIndexes()方法。

  • 从集合中删除指定索引。要指定要删除的多个索引,请向该方法传递一个索引名称数组:

    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:

Parameter
类型
说明

indexes

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

可选。指定要删除的一个或多个索引。

要从集合中删除 _id 索引以外的所有索引,请省略该参数。

要删除单个索引,请指定索引名称、索引规范文档(除非该索引是文本索引)或索引名称的数组。要删除文本索引,请指定索引名称或索引名称的数组,而不是索引规范文档。

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

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

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

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

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

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

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

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

给本页内容打分

在此页面上