On this page本页内容
db.collection.hideIndex()
New in version 4.4.在版本4.4中新增。
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
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.
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.有关详细信息,请参阅隐藏索引。
db.collection.hideIndex(<index>)
The db.collection.hideIndex()
method takes the following parameter:db.collection.hideIndex()
方法采用以下参数:
index | string or document |
|
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. However, once hidden, the index remains hidden even with featureCompatibilityVersion set to 4.2
on MongoDB 4.4 binaries.featureCompatibilityVersion
设置为4.4
或更高。然而,一旦隐藏,即使在MongoDB 4.4二进制文件中将featureCompatibilityVersion
设置为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()
创建索引而不隐藏:
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
索引选项。