带驱动程序的 MongoDB
定义
db.collection.getIndexes()返回一个大量,其中包含标识和描述集合上现有索引(包括隐藏索引和当前正在构建的索引)的文档列表。
You must call
db.collection.getIndexes()on a collection. For example:db.collection.getIndexes() 将
collection更改为要返回索引信息的集合的名称。
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
Atlas Search 索引
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 操作需要成员处于 PRIMARY 或 SECONDARY 状态。如果节点处于其他状态,如 STARTUP2,则操作错误。
通配符索引 (Wildcard Indexes)
从 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()。