MongoDB getIndexes() Method (original) (raw)

Last Updated : 5 May, 2026

The getIndexes() method in MongoDB retrieves detailed information about all indexes on a collection. It helps developers review index keys and options to manage and optimize query performance effectively.

Syntax

db.collection_name.getIndexes()

Return Value

The getIndexes() method returns an array of documents, where each document represents an index and contains details such as:

Examples of $getIndexes in MongoDB

In these examples, we will work with the gfg databases and the student collection, which contains documents with the fields name and language.

Screenshot-2026-02-18-115638

Example 1: Retrieve Index Information for a Collection

To view the existing indexes in the student collection, use the getIndexes() method:

db.student.getIndexes()

**Output:

Screenshot-2026-02-18-115802

Example 2: Create an Index Using createIndex() and View with getIndexes()

Create an index on the name and language fields of the student collection.

db.student.createIndex({name:1, language:-1})

**Output:

Screenshot-2026-02-18-115914

**Query:

db.student.getIndexes()

**Output:

Screenshot-2026-02-18-120358

Example 3: Viewing Hidden Indexes

In MongoDB 4.4+, hidden indexes can be viewed with getIndexes() and are ignored by the query planner unless explicitly used, and they can be created by setting hidden: true during index creation.

db.student.createIndex({ email: 1 }, { hidden: true, name: "email_hidden_index" })

Now, to view the hidden index, use the getIndexes() method:

db.student.getIndexes()

**Output:

Screenshot-2026-02-18-120700