Using the getIndexes() Function in MongoDB

In this guide, I will explain how to use the getIndexes() Function in MongoDB.

A list of documents describing the existing indexes in the collection includes hidden indexes, and is returned as an array by the getIndexes() function.

This guide aims to throw light on a examples that illustrate the usage of the getIndexes() function.

So, let us get this guide started.

The getIndexes() Function Syntax in MongoDB

Let us take a quick look at the getIndexes() function syntax in MongoDB:

db.collection.getIndexes()
  • This method accepts no parameters.
  • This method returns index information that includes the keys and options used to create an index.
  • The hidden index is available in this method beginning with MongoDB 4.4. If the value is true, nothing else will happen.

Using the getIndexes() function in MongoDB

Let us get started by taking a look at a small example to use the getIndexes() function in MongoDB. We’ll use pretty print in MongoDB.

Here is the collection I would like to use for the guide:

> db.movies.find().pretty()
{
        "_id" : ObjectId("60322d3501cd70079c48cb65"),
        "title" : "Enchanted",
        "year" : 2006,
        "score" : 10,
        "rating" : "PG",
        "__v" : 0
}
{
        "_id" : ObjectId("60322d3501cd70079c48cb67"),
        "title" : "Final Destination II",
        "year" : 2015,
        "score" : 10,
        "rating" : "PG-13",
        "__v" : 0
}
{
        "_id" : ObjectId("6190189ef5c8903629012fe1"),
        "title" : "Fifty Shades of Grey",
        "year" : 2015,
        "score" : 10,
        "rating" : "NC-17",
        "__v" : 0
}
{
        "_id" : ObjectId("6190189ef5c8903629012fe2"),
        "title" : "Cars",
        "year" : 2006,
        "score" : 8,
        "rating" : null,
        "__v" : 0
}
{
        "_id" : ObjectId("6190189ef5c8903629012fe3"),
        "title" : "The Matrix",
        "year" : 1999,
        "score" : null,
        "rating" : "R",
        "__v" : 0
}
{
        "_id" : ObjectId("61901f82f5c8903629012fe4"),
        "title" : "Salt",
        "year" : 2010,
        "score" : 9,
        "rating" : "",
        "__v" : 0
}
{
        "_id" : ObjectId("61901f82f5c8903629012fe5"),
        "title" : "Knowing",
        "year" : 2009,
        "score" : 8,
        "rating" : "",
        "__v" : 0
}

Printing the Index Information of the Sample Collection Using the getIndexes() Function

Let us now proceed with printing out the index information of the sample collection.

> db.movies.getIndexes()
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]

Note:

Before you perform this function, you must create an index for your collection. You can only do it one time for a given collection. To create an index for your collection, follow the further steps.

Creating an Index for a Collection

Let us now see how we can create an index for a collection to be able to use the getIndexes() function.

I’ll be using the same collection as we used above:

> db.movies.find().pretty()
{
        "_id" : ObjectId("60322d3501cd70079c48cb65"),
        "title" : "Enchanted",
        "year" : 2006,
        "score" : 10,
        "rating" : "PG",
        "__v" : 0
}
{
        "_id" : ObjectId("60322d3501cd70079c48cb67"),
        "title" : "Final Destination II",
        "year" : 2015,
        "score" : 10,
        "rating" : "PG-13",
        "__v" : 0
}
{
        "_id" : ObjectId("6190189ef5c8903629012fe1"),
        "title" : "Fifty Shades of Grey",
        "year" : 2015,
        "score" : 10,
        "rating" : "NC-17",
        "__v" : 0
}
{
        "_id" : ObjectId("6190189ef5c8903629012fe2"),
        "title" : "Cars",
        "year" : 2006,
        "score" : 8,
        "rating" : null,
        "__v" : 0
}
{
        "_id" : ObjectId("6190189ef5c8903629012fe3"),
        "title" : "The Matrix",
        "year" : 1999,
        "score" : null,
        "rating" : "R",
        "__v" : 0
}
{
        "_id" : ObjectId("61901f82f5c8903629012fe4"),
        "title" : "Salt",
        "year" : 2010,
        "score" : 9,
        "rating" : "",
        "__v" : 0
}
{
        "_id" : ObjectId("61901f82f5c8903629012fe5"),
        "title" : "Knowing",
        "year" : 2009,
        "score" : 8,
        "rating" : "",
        "__v" : 0
}

Here’s how to create an index for a collection:

> db.movies.createIndex({rating: 1, title: 1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

Let us now do the getIndexes() function again:

> db.movies.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "rating" : 1,
                        "title" : 1
                },
                "name" : "rating_1_title_1"
        }
]

You must have noticed that this time I received a longer array because I created an index a second time for this collection.

Conclusion

Learn to use the getIndexes() function in MongoDB with examples as illustrated here.

Noteworthy References

https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex

Aneesha S
Aneesha S
Articles: 172