Docs Home → Develop Applications → MongoDB Manual
db.collection.unhideIndex()![](/zh-cn/docs/v5.3/assets/link.svg)
On this page
Definition![](/zh-cn/docs/v5.3/assets/link.svg)
db.collection.unhideIndex()
Important
mongosh Method
This is a
mongosh
method. This is not the documentation forNode.js
or other programming language specific driver methods.In most cases,
mongosh
methods work the same way as the legacymongo
shell methods. However, some legacy methods are unavailable inmongosh
.For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Unhides an existing index from the query planner. Once unhidden, the indexes are immediately available for use.
Syntax![](/zh-cn/docs/v5.3/assets/link.svg)
db.collection.unhideIndex(<index>)
Parameters![](/zh-cn/docs/v5.3/assets/link.svg)
The db.collection.unhideIndex()
method takes the following
parameter:
Parameter | Type | Description |
---|---|---|
index | string or document | Specifies the index to unhide from the query planner. You can specify the index either by the index name or by the index specification document. TipYou can use the To unhide a text index, specify the index name. |
The db.collection.unhideIndex()
is a mongo
shell
wrapper for the collMod
command.
Behavior![](/zh-cn/docs/v5.3/assets/link.svg)
Index Modifications Reset Statistics![](/zh-cn/docs/v5.3/assets/link.svg)
Unhiding a hidden index resets its $indexStats
.
No-op![](/zh-cn/docs/v5.3/assets/link.svg)
Unhiding an already unhidden index has no effect on the index. However, the operation will still generate an empty oplog entry.
Required Access![](/zh-cn/docs/v5.3/assets/link.svg)
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![](/zh-cn/docs/v5.3/assets/link.svg)
The following example unhides an existing index.
First, use db.collection.createIndex()
to create a hidden
index:
db.restaurants.createIndex( { borough: 1, ratings: 1 }, { hidden: true } );
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 the index, you can specify either the index key specification
document or the index name to the db.collection.unhideIndex()
method. The following specifies the index name:
db.restaurants.unhideIndex( "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" } ]
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
.