On this page本页内容
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.有关详细信息,请参阅隐藏索引。
The db.collection.hideIndex()
method takes the following parameter:db.collection.hideIndex()
方法采用以下参数:
index |
string or document |
Tip
|
The db.collection.hideIndex()
is a mongo
shell wrapper for the collMod
command.db.collection.hideIndex()
是collMod
命令的mongo
shell包装器。
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 然而,一旦隐藏,即使在MongoDB 4.4二进制文件上featureCompatibilityVersion设置为4.2
on MongoDB 4.4 binaries."4.2"
,索引仍然隐藏。
You cannot hide the 无法隐藏_id
index._id
索引。
Hiding an unhidden index resets its 隐藏未隐藏的索引将重置其$indexStats
.$indexStats
。
Hiding an already hidden index has no effect on the index. 隐藏已隐藏的索引对索引没有影响。However, the operation will still generate an empty oplog entry.但是,该操作仍将生成一个空的oplog条目。
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
提供所需的权限。
The following example hides an existing index.下面的示例隐藏了一个现有索引。
First, use 首先,使用db.collection.createIndex()
to create an index without hiding:db.collection.createIndex()
创建索引而不隐藏:
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:以下内容指定了索引名称:
To verify, run 要进行验证,请在db.collection.getIndexes()
on the restaurants
collection:restaurants
集合上运行db.collection.getIndexes()
:
The operation returns the following information:该操作返回以下信息:
The 只有当值为hidden
index option is only returned if the value is true
.true
时,才会返回hidden
索引选项。