Docs HomeMongoDB Manual

db.collection.getIndexes()

Definition定义

db.collection.getIndexes()
Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the listIndexes command.

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

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

mongo shell v4.4

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 db.collection.getIndexes() disconnects before the operation completes, MongoDB marks db.collection.getIndexes() for termination using killOp.从MongoDB 4.2开始,如果发出db.collection.getIndexes()的客户端在操作完成前断开连接,MongoDB会使用killOp标记db.collection.getIndexes()终止。

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 also run when the member is in STARTUP2. The operations wait until the member transitioned to RECOVERING.在以前的版本中,成员处于STARTUP2时也会运行这些操作。操作将等待,直到成员转换到RECOVERING

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()命令的输出有所不同。

Atlas Search IndexesAtlas搜索索引

getIndexes() does not return information on Atlas Search indexes.不会返回有关Atlas Search索引的信息。

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:例如:

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()