- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.hideIndex()
db.collection.hideIndex()¶
On this page
Definition¶
-
db.collection.
hideIndex
()¶ New in version 4.4.
mongo
Shell MethodThis page documents the
mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.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.
Syntax¶
Parameters¶
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 To hide a text index, specify the index name. |
The db.collection.hideIndex()
is a mongo
shell
wrapper for the collMod
command.
Behavior¶
Feature Compatibility Version¶
To hide an index, you must have featureCompatibilityVersion set to 4.4
or greater. However, once hidden, the index
remains hidden even with featureCompatibilityVersion
set to 4.2
on MongoDB 4.4 binaries.
Restrictions¶
You cannot hide the _id
index.
Index Modifications Reset Statistics¶
Hiding an unhidden index resets its $indexStats
.
No-op¶
Hiding an already hidden index has no effect on the index. However, the operation will still generate an empty oplog entry.
Access Control¶
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.
Example¶
The following example hides an existing index.
First, use db.collection.createIndex()
to create an index
without hiding:
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:
To verify, run db.collection.getIndexes()
on the
restaurants
collection:
The operation returns the following information:
The hidden
index option is only returned if the value is true
.