Database Manual / Reference / mongosh Methods / Collections

db.collection.getIndexes() (mongosh method方法)

Definition定义

db.collection.getIndexes()

Returns an array that holds a list of documents that identify and describe the existing indexes on the collection, including hidden indexes and indexes that are currently being built.返回一个数组,其中包含标识和描述集合上现有索引的文档列表,包括隐藏索引和当前正在构建的索引。

You must call db.collection.getIndexes() on a collection. For example:您必须对集合调用db.collection.getIndexes()。例如:

db.collection.getIndexes()

Change collection to the name of the collection for which to return index information.collection更改为要返回其索引信息的集合的名称。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Behavior行为

MongoDB Search IndexesMongoDB搜索索引

getIndexes() does not return information on MongoDB Search indexes. For information on Atlas Search indexes, use $listSearchIndexes.getIndexes()不返回有关MongoDB搜索索引的信息。有关Atlas搜索索引的信息,请使用$listSearchIndexes

Client Disconnection客户端断开连接

If the client that issued db.collection.getIndexes() disconnects before the operation completes, MongoDB marks db.collection.getIndexes() for termination using killOp.如果发出db.collection.getIndexes()的客户端在操作完成之前断开连接,MongoDB将使用killOp标记db.collection.getIndexes()以终止。

Replica Set Member State Restriction副本集成员状态限制

To run on a replica set member, listIndexes operations require the member to be in PRIMARY or SECONDARY state. If the member is in another state, such as STARTUP2, the operation errors.要在副本集成员上运行,listIndexes操作要求该成员处于PRIMARYSECONDARY状态。如果成员处于另一种状态,如STARTUP2,则操作错误。

Wildcard Indexes通配符索引

Starting in MongoDB 6.3, 6.0.5, and 5.0.16, the wildcardProjection field stores the index projection in its submitted form. Earlier versions of the server may have stored the projection in a normalized form.从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.服务器以相同的方式使用索引,但您可能会注意到listIndexesdb.collection.getIndexes()命令的输出存在差异。

Required Access所需访问权限

To run db.collection.getIndexes() when access control is enforced, users must have privileges to listIndexes on the collection.要在实施访问控制时运行db.collection.getIndexes(),用户必须在集合上具有listIndexes的权限。

The built-in role read provides the required privileges to run db.collection.getIndexes() for the collections in a database.内置的角色read提供了为数据库中的集合运行db.collection.getIndexes()所需的权限。

Output输出

db.collection.getIndexes() returns an array of documents that hold index information for the collection. For example:返回一个包含集合索引信息的文档数组。例如:

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

Index information includes the keys and options used to create the index. The index option hidden is only available if the value is true.索引信息包括用于创建索引的键和选项。hidden的索引选项仅在值为true时可用。

For information on the keys and index options, see db.collection.createIndex().有关键和索引选项的信息,请参阅db.collection.createIndex()