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

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

带驱动程序的 MongoDB

This page documents a mongosh method. To see the equivalent method in a MongoDB driver, see the corresponding page for your programming language:
db.collection.getIndexes()

返回一个大量,其中包含标识和描述集合上现有索引(包括隐藏索引和当前正在构建的索引)的文档列表。

You must call db.collection.getIndexes() on a collection. For example:

db.collection.getIndexes()

collection 更改为要返回索引信息的集合的名称。

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

getIndexes() does not return information on Atlas Search indexes. For information on Atlas Search indexes, use $listSearchIndexes.

If the client that issued db.collection.getIndexes() disconnects before the operation completes, MongoDB marks db.collection.getIndexes() for termination using killOp.

要在副本集成员上运行,listIndexes 操作需要成员处于 PRIMARYSECONDARY 状态。如果节点处于其他状态,如 STARTUP2,则操作错误。

从 MongoDB 6.3、6.0.5 和 5.0.16 开始,wildcardProjection 字段以其提交的形式存储索引投影。早期版本的服务器可能已将投影以标准化形式存储。

The server uses the index the same way, but you may notice a difference in the output of the listIndexes and db.collection.getIndexes() commands.

To run db.collection.getIndexes() when access control is enforced, users must have privileges to listIndexes on the collection.

The built-in role read provides the required privileges to run db.collection.getIndexes() for the collections in a database.

db.collection.getIndexes() 会返回保存该集合索引信息的文档的数组。例如:

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"status" : 1
},
"name" : "status_1"
},
{
"v" : 2,
"key" : {
"points" : 1
},
"name" : "points_1"
}
]

索引信息包括用于创建索引的键和选项。仅当值为 true 时,索引选项 hidden 才可用。

有关键和索引选项的信息,请参阅db.collection.createIndex()