db.collection.getIndexes()

On this page本页内容

Definition定义

db.collection.getIndexes()
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

For MongoDB API drivers, refer to the language specific MongoDB driver documentation.

Returns an array that holds a list of documents that identify and describe the existing indexes on the collection, including hidden indexes. 返回一个数组,其中包含标识和描述集合上现有索引(包括隐藏索引)的文档列表。You must call db.collection.getIndexes() on a collection. 必须对集合调用db.collection.getIndexes()For example:例如:

db.collection.getIndexes()

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

Behavior行为

Client Disconnection客户端断开连接

Starting in MongoDB 4.2, if the client that issued the db.collection.getIndexes() disconnects before the operation completes, MongoDB marks the db.collection.getIndexes() for termination (i.e. killOp on the operation).从MongoDB 4.2开始,如果发出db.collection.getIndexes()的客户端在操作完成之前断开连接,MongoDB将db.collection.getIndexes()标记为终止(即操作上的killOp)。

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

Starting in MongoDB 4.4, to run on a replica set member, listIndexes operations require the member to be in PRIMARY or SECONDARY state. 从MongoDB 4.4开始,要在副本集成员上运行,listIndexes操作要求该成员处于PRIMARYSECONDARY状态。If the member is in another state, such as STARTUP2, the operation errors.如果成员处于其他状态,如STARTUP2,则操作出错。

In previous versions, the operations can also be run when the member is in STARTUP2. 在以前的版本中,也可以在成员处于STARTUP2时运行操作。However, the operations wait until the member transitions to RECOVERING.但是,这些操作将一直等到成员转换到RECOVERING

Required Access所需访问权限

To run db.collection.getIndexes() when access control is enforced, usesrs 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:例如:

Note注意

Starting in MongoDB 4.4, db.collection.getIndexes() no longer includes the ns field.从MongoDB 4.4开始,db.collection.getIndexes()不再包含ns字段。

[
   {
      "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, available starting in MongoDB 4.4, is only available if the value is true.索引选项hidden从MongoDB 4.4开始可用,只有当值为true时才可用。

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

←  db.collection.findOneAndUpdate()db.collection.getShardDistribution() →