How to Check Whether an Index is Hidden in MongoDB

From MongoDB 4.4, it’s possible to hide an index from the query planner. This allows you to evaluate the potential impact of dropping an index without actually dropping the index.

You can use the getIndexes() method to check whether or not an index is hidden. If an index is hidden, that index will display a hidden field as having a value of true (i.e. "hidden" : true).

Example

Here’s an example of calling getIndexes() to return all indexes on a collection called pets:

db.pets.getIndexes()

Result:

[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_"
	},
	{
		"v" : 2,
		"key" : {
			"name" : 1,
			"type" : -1
		},
		"name" : "idx_name_1_type_-1"
	},
	{
		"v" : 2,
		"key" : {
			"weight" : -1
		},
		"name" : "idx_weight_-1",
		"hidden" : true
	}
]

We can see that there are three indexes, and the third one is hidden. We know this because it has "hidden" : true.