Definition定义
db.collection.hideIndex()-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. 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.hiddencollection option set using thecollModcommand.collMod命令设置的index.hidden集合选项。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关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, you can evaluate the potential impact of dropping an index without actually dropping the index. If the impact is negative, you 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.有关详细信息,请参阅隐藏索引。
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. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关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.hideIndex(<index>)
Parameters参数
The db.collection.hideIndex() method takes the following parameter:db.collection.hideIndex()方法接受以下参数:
index |
|
The db.collection.hideIndex() is a mongosh shell wrapper for the collMod command.db.collection.hideIndex()是collMod命令的mongosh shell包装器。
Behavior行为
Feature Compatibility Version功能兼容性版本
To hide an index, you must have featureCompatibilityVersion set to 要隐藏索引,必须将6.0 or greater.featureCompatibilityVersion设置为6.0或更高版本。
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. The following specifies the index name:db.collection.hideIndex()方法的索引名称。以下指定了索引名称:
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索引选项。