2 Ways to Unhide an Index in MongoDB

If you have a hidden index in MongoDB, you can use the unhideIndex() method or the collMod administration command to unhide it.

Example Indexes

Let’s take a look at the indexes on a collection called pets:

db.pets.getIndexes()

Result:

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

We can see that there are three indexes, and the last two are hidden. We know this because they have "hidden" : true in their definition.

Unhide using the unhideIndexes() Method

The first way to unhide an index is to use the db.collection.unhideIndex() method. This method accepts the name of the index or its key pattern as a parameter to specify which index to unhide.

Example:

db.pets.unhideIndex("idx_weight_-1")

Output:

{ "hidden_old" : true, "hidden_new" : false, "ok" : 1 }

The output of the unhideIndex() method displays the old value for the hidden field (in this case true) and the new value (in this case false).

However, if we unhide an already unhidden index (or hide an already hidden one), these aren’t displayed, and we just get the following:

{ "ok" : 1 }

In any case, the index is now unhidden.

The unhideIndex() method is a wrapper for the collMod administration command (below).

The collMod Command

The collMod administration command enables us to add options to a collection or to modify view definitions.

We can use it to unhide an index by passing hidden: false:

Example:

db.runCommand( {
   collMod: "pets",
   index: {
      name: "type_1",
      hidden: false
   }
} )

Result:

{ "hidden_old" : true, "hidden_new" : false, "ok" : 1 }

This returns the same document that unhideIndex() returns.

Similar to unhideIndex(), you have the option of specifying the index name or its key pattern.

Here’s an example of using the key pattern:

db.runCommand( {
   collMod: "pets",
   index: {
      keyPattern: { "type" : 1 },
      hidden: false
   }
} )

In this case, the index was defined using { "type" : 1 }, and so this definition can be used instead of the index name.

Verify the Changes

To verify our changes, we can call getIndexes() again to see the index’s definition:

db.pets.getIndexes()

Result:

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

As expected, both indexes have been unhidden. We can also hide them again by passing hidden: true.

Can’t Hide/Unhide Indexes? Check this Setting.

The mongod featureCompatibilityVersion must be at least 4.4 before you can hide indexes. However, once hidden, an index will remain hidden even with featureCompatibilityVersion set to 4.2 on MongoDB 4.4 binaries.

You can check the featureCompatibilityVersion setting with the following code:

db.adminCommand( 
    { 
        getParameter: 1, 
        featureCompatibilityVersion: 1 
    } 
)

You can set it using the setFeatureCompatibilityVersion command:

db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )

The setFeatureCompatibilityVersion command needs to be run in the admin database.

Also note, you cannot hide the _id index.