Docs Home → Develop Applications → MongoDB Manual
db.collection.getIndexes()![](/zh-cn/docs/v5.3/assets/link.svg)
On this page
Definition![](/zh-cn/docs/v5.3/assets/link.svg)
db.collection.getIndexes()
Important
mongosh Method
This is a
mongosh
method. This is not the documentation forNode.js
or other programming language specific driver methods.In most cases,
mongosh
methods work the same way as the legacymongo
shell methods. However, some legacy methods are unavailable inmongosh
.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. For example:db.collection.getIndexes() Change
collection
to the name of the collection for which to return index information.
Behavior![](/zh-cn/docs/v5.3/assets/link.svg)
Client Disconnection![](/zh-cn/docs/v5.3/assets/link.svg)
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
.
Replica Set Member State Restriction![](/zh-cn/docs/v5.3/assets/link.svg)
Starting in MongoDB 4.4, 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.
In previous versions, the operations also run when the member
is in STARTUP2
. The operations wait until the member
transitioned to RECOVERING
.
Required Access![](/zh-cn/docs/v5.3/assets/link.svg)
To run db.collection.getIndexes()
when access control is
enforced, usesrs 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.
Output![](/zh-cn/docs/v5.3/assets/link.svg)
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.
[ { "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
.
For information on the keys and index options, see
db.collection.createIndex()
.