Docs Menu
Docs Home
/
MongoDB Manual
/ /

Hidden Indexes

On this page

  • Behavior
  • Restrictions
  • Examples

Hidden indexes are not visible to the query planner and cannot be used to support a query.

By hiding an index from the planner, you can evaluate the potential impact of dropping an index without actually dropping the index. If the impact is negative, you can unhide the index instead of having to recreate a dropped index.

Apart from being hidden from the planner, hidden indexes behave like unhidden indexes; i.e.

To create a hidden index, use the db.collection.createIndex() method with the hidden option set to true.

Note

To use the hidden option with db.collection.createIndex(), you must have featureCompatibilityVersion set to 5.0 or greater.

For example, the following operation creates a hidden ascending index on the borough field:

db.addresses.createIndex(
{ borough: 1 },
{ hidden: true }
);

To verify, run db.collection.getIndexes() on the addresses collection:

db.addresses.getIndexes()

The operation returns the following information:

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"borough" : 1
},
"name" : "borough_1",
"hidden" : true
}
]

The index option hidden is only returned if the value is true.

Note

To hide an existing index, you can use the collMod command or mongosh helper db.collection.hideIndex().

For example, create an index without hiding:

db.restaurants.createIndex( { borough: 1, ratings: 1 } );

To hide the index, you can specify either:

  • the index key specification document to the db.collection.hideIndex() method:

    db.restaurants.hideIndex( { borough: 1, ratings: 1 } ); // Specify the index key specification document
  • the index name to the db.collection.hideIndex() method:

    db.restaurants.hideIndex( "borough_1_ratings_1" ); // Specify the index name

To verify, run db.collection.getIndexes() on the restaurants collection:

db.restaurants.getIndexes()

The operation returns the following information:

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"borough" : 1,
"ratings" : 1
},
"name" : "borough_1_ratings_1",
"hidden" : true
}
]

The index option hidden is only returned if the value is true.

To unhide a hidden index, you can use the collMod command or mongosh helper db.collection.unhideIndex(). You can specify either:

  • the index key specification document to the db.collection.unhideIndex() method:

    db.restaurants.unhideIndex( { borough: 1, city: 1 } ); // Specify the index key specification document
  • the index name to the db.collection.unhideIndex() method:

    db.restaurants.unhideIndex( "borough_1_ratings_1" ); // Specify the index name

To verify, run db.collection.getIndexes() on the restaurants collection:

db.restaurants.getIndexes()

The operation returns the following information:

[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"borough" : 1,
"ratings" : 1
},
"name" : "borough_1_ratings_1"
}
]

The index option hidden no longer appears as part of the borough_1_ratings_1 index since the field is only returned if the value is true.

Because indexes are fully maintained while hidden, the index is immediately available for use once unhidden.

Back

Case-Insensitive Indexes