db.collection.hideIndex()

On this page本页内容

Definition定义

db.collection.hideIndex()

New in version 4.4.版本4.4中的新功能。

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

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语法

db.collection.hideIndex(<index>)

Parameters参数

The db.collection.hideIndex() method takes the following parameter:db.collection.hideIndex()方法采用以下参数:

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.可以使用db.collection.getIndexes()方法查找索引名称或索引规范文档。

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

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

Behavior行为

Feature Compatibility Version功能兼容性版本

To hide an index, you must have featureCompatibilityVersion set to 4.4 or greater. 要隐藏索引,必须将功能兼容性版本设置为"4.4"或更高。However, once hidden, the index remains hidden even with featureCompatibilityVersion set to 4.2 on MongoDB 4.4 binaries.然而,一旦隐藏,即使在MongoDB 4.4二进制文件上featureCompatibilityVersion设置为"4.2",索引仍然隐藏。

Restrictions限制

You cannot hide the _id index.无法隐藏_id索引。

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

Hiding an unhidden index resets its $indexStats.隐藏未隐藏的索引将重置其$indexStats

No-op无操作

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

Access Control访问控制

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 hides an existing index.下面的示例隐藏了一个现有索引。

First, use db.collection.createIndex() to create an index without hiding:首先,使用db.collection.createIndex()创建索引而不隐藏:

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. 要隐藏索引,可以指定索引键规范文档或db.collection.hideIndex()方法的索引名。The following specifies the index name:以下内容指定了索引名称:

db.restaurants.hideIndex( "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",
"hidden" : true   }
]

The hidden index option is only returned if the value is true.只有当值为true时,才会返回hidden索引选项。