Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

db.collection.hideIndex()

On this page

  • Definition
  • Syntax
  • Behavior
  • Access Control
  • Example
db.collection.hideIndex()

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the index.hidden collection option set using the collMod command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

Hides an existing index from the query planner. An index hidden from the query planner is not evaluated as part of query plan selection.

By hiding an index from the planner, users can evaluate the potential impact of dropping an index without actually dropping the index. If the impact is negative, the user can unhide the index instead of having to recreate a dropped index. And because indexes are fully maintained while hidden, the indexes are immediately available for use once unhidden.

For details, see Hidden Indexes.

db.collection.hideIndex(<index>)

The db.collection.hideIndex() method takes the following parameter:

Parameter
Type
Description
index
string or document

Specifies the index to hide from the query planner. You can specify the index either by the index name or by the index specification document.

Tip

You can use the db.collection.getIndexes() method to find the index name or the index specification document.

To hide a text index, specify the index name.

The db.collection.hideIndex() is a mongosh shell wrapper for the collMod command.

To hide an index, you must have featureCompatibilityVersion set to 5.0 or greater.

You cannot hide the _id index.

Hiding an unhidden index resets its $indexStats.

Hiding an already hidden index has no effect on the index. However, the operation will still generate an empty oplog entry.

If the deployment enforces authentication/authorization, you must have the collMod privilege in the collection's database.

The built-in role dbAdmin provides the required privileges.

The following example hides an existing index.

First, use db.collection.createIndex() to 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 or the index name to the db.collection.hideIndex() method. The following specifies the index name:

db.restaurants.hideIndex( "borough_1_ratings_1" );

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 hidden index option is only returned if the value is true.

Tip

See also:

←  db.collection.getShardVersion()db.collection.insert() →