重要
mongosh 方法
This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.
有关该数据库命令,请参阅使用 collMod 命令设置的 index.hidden 集合选项。
如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
定义
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
db.collection.unhideIndex(<index>)
参数
The db.collection.unhideIndex() method takes the following parameter:
Parameter | 类型 | 说明 |
|---|---|---|
| 字符串或文档 | 指定要在查询规划器中取消隐藏的索引。 您可以通过索引名称或索引规范文档来指定索引。 要查找索引名称或索引规范文档,可以使用 要取消隐藏文本索引,请指定索引名称。 |
The db.collection.unhideIndex() is a mongosh shell wrapper for the collMod command.
行为
索引修改重置统计信息
取消隐藏隐藏索引会重置其$indexStats 。
No-op
取消隐藏已取消隐藏的索引对该索引没有影响。 但是,该操作仍会生成空的 oplog 条目。
必需的访问权限
如果部署强制执行身份验证/授权,则您必须在集合的数据库中拥有 collMod 权限。
内置角色dbAdmin提供所需的特权。
例子
以下示例取消隐藏现有索引。
首先,使用db.collection.createIndex()创建隐藏索引:
db.restaurants.createIndex( { borough: 1, ratings: 1 }, { hidden: true } );
要进行验证,请在 restaurants 集合上运行 db.collection.getIndexes():
db.restaurants.getIndexes();
操作会返回以下信息:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "borough" : 1, "ratings" : 1 }, "name" : "borough_1_ratings_1", "hidden" : true } ]
仅当值为 true 时才返回索引选项 hidden(隐藏)。
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" );
要进行验证,请在 restaurants 集合上运行 db.collection.getIndexes():
db.restaurants.getIndexes()
操作会返回以下信息:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "borough" : 1, "ratings" : 1 }, "name" : "borough_1_ratings_1" } ]
索引选项 hidden 不再显示为 borough_1_ratings_1 索引的一部分,因为仅当值为 true 时才返回该字段。