Database Manual / Reference / mongosh Methods / Collections

db.collection.unhideIndex() (mongosh method方法)

Definition定义

db.collection.unhideIndex()

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.本页记录了一种mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。

For the database command, see the index.hidden collection option set using the collMod command.对于数据库命令,请参阅使用collMod命令设置的index.hidden集合选项。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Unhides an existing index from the query planner. Once unhidden, the indexes are immediately available for use.从查询计划器中取消隐藏现有索引。一旦取消隐藏,索引即可立即使用。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

db.collection.unhideIndex(<index>)

Parameters参数

The db.collection.unhideIndex() method takes the following parameter:db.collection.unhideIndex()方法接受以下参数:

Parameter参数Type类型Description描述
indexstring 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.指定要从查询计划器中取消隐藏的索引。您可以通过索引名称或索引规范文档指定索引。

To find the index name or index specification document, you can use the db.collection.getIndexes() method.要查找索引名称或索引规范文档,可以使用db.collection.getIndexes()方法。

To unhide a text index, specify the index name.要取消隐藏text索引,请指定索引名称。

The db.collection.unhideIndex() is a mongosh shell wrapper for the collMod command.db.collection.unhideIndex()collMod命令的mongosh shell包装器。

Behavior行为

Index Modifications Reset Statistics索引修改重置统计信息

Unhiding a hidden index resets its $indexStats.取消隐藏隐藏索引会重置其$indexStats

No-op

Unhiding an already unhidden index has no effect on the index. However, the operation will still generate an empty oplog entry.取消隐藏已取消隐藏的索引对索引没有影响。但是,该操作仍将生成一个空的oplog条目。

Required Access所需访问权限

If the deployment enforces authentication/authorization, you must have the collMod privilege in the collection's database.如果部署强制执行身份验证/授权,则您必须在集合的数据库中拥有collMod权限。

The built-in role dbAdmin provides the required privileges.内置角色dbAdmin提供所需的权限。

Example示例

The following example unhides an existing index.以下示例取消隐藏现有索引。

First, use db.collection.createIndex() to create a hidden index:首先,使用db.collection.createIndex()创建一个隐藏索引:

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

To verify, run db.collection.getIndexes() on the restaurants collection:要验证,请在restaurants集合上运行db.collection.getIndexes()

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.只有当值为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.collection.unhideIndex()方法的索引名称。以下指定了索引名称:

db.restaurants.unhideIndex( "borough_1_ratings_1" );

To verify, run db.collection.getIndexes() on the restaurants collection:要验证,请在restaurants集合上运行db.collection.getIndexes()

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.hidden索引选项不再显示为borough_1_ratings_1索引的一部分,因为该字段仅在值为true时返回。