Open In App

MongoDB – getIndexes() Method

Improve
Improve
Like Article
Like
Save
Share
Report

In MongoDB, the getIndexes() method returns an array that contains a list of documents that identify and describe the existing indexes on the specified collection. It also includes hidden indexes as well. 

  • This method does not take any parameters.
  • The index information return by this method contains the keys and the options used to create an index.
  • In this method, the hidden index available starting from MongoDB 4.4. Only if the value is true.

Syntax:

db.Collection_name.getIndexes()

Return:

This method returns an array that contains a list of documents that identify and describe the existing indexes on the specified collection. It also includes hidden indexes as well.

Examples:

In the following examples, we are working with:

Database: gfg

Collection: student

Documents: Three documents contains name and the language in which the students are interested.

  • Return an array of documents that hold index information for the student collection:
db.student.getIndexes()

  • Create an index using createIndex() method:
db.student.createIndex({name:1, language:-1})

Here, we first create an index using createIndex() method.

After creating a new index we use greateIndex() method to find the index information for the student collection.

db.student.getIndexes()


Last Updated : 05 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads