On this page本页内容
db.collection.getIndexes()
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
更改为要返回其索引信息的集合的名称。
Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.collection.getIndexes()
disconnects before the operation completes, MongoDB marks the db.collection.getIndexes()
for termination (i.e. killOp
on the operation).db.collection.getIndexes()
的客户端在操作完成之前断开连接,MongoDB将db.collection.getIndexes()
标记为终止(即操作上的killOp
)。
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,listIndexes
operations require the member to be in PRIMARY
or SECONDARY
state. listIndexes
操作要求该成员处于PRIMARY
或SECONDARY
状态。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
。
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()
所需的权限。
db.collection.getIndexes()
returns an array of documents that hold index information for the collection. 返回保存集合索引信息的文档数组。For example:例如:
Starting in MongoDB 4.4, 从MongoDB 4.4开始,db.collection.getIndexes()
no longer includes the ns
field.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()
。