重要
mongosh 方法
This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.
有关该数据库命令,请参阅使用 collMod 命令设置的 index.hidden 集合选项。
如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
定义
db.collection.hideIndex()向查询规划器隐藏现有索引。向查询规划器隐藏的索引不会作为查询计划选择的一部分进行求值。
通过向规划器隐藏索引,您可以在不实际删除索引的情况下评估删除索引的潜在影响。 如果影响是负面的,您可以取消隐藏索引,而不必重新创建已删除的索引。 由于索引在隐藏期间得到完全维护,因此一旦取消隐藏,索引就立即可用。
有关详情,请参阅隐藏索引。
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
db.collection.hideIndex(<index>)
参数
The db.collection.hideIndex() method takes the following parameter:
Parameter | 类型 | 说明 |
|---|---|---|
| 字符串或文档 | 指定要向查询规划器隐藏的索引。可以通过索引名称或索引规范文档来指定索引。 要查找索引名称或索引规范文档,可以使用 要隐藏文本索引,请指定索引名称。 |
The db.collection.hideIndex() is a mongosh shell wrapper for the collMod command.
行为
特征兼容性版本
要隐藏索引,必须将 featureCompatibilityVersion 设置为 7.0 或更大。
限制
您无法隐藏 _id 索引。
索引修改重置统计信息
隐藏未隐藏的索引会重置其 $indexStats。
No-op
隐藏已隐藏的索引对该索引没有影响。但是,该操作仍然会生成一个空的 oplog 条目。
访问控制
如果部署强制执行身份验证/授权,则您必须在集合的数据库中拥有 collMod 权限。
内置角色dbAdmin提供所需的特权。
例子
以下示例隐藏现有索引。
首先,使用 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.restaurants.hideIndex( "borough_1_ratings_1" );
要进行验证,请在 restaurants 集合上运行 db.collection.getIndexes():
db.restaurants.getIndexes();
操作会返回以下信息:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "borough" : 1, "ratings" : 1 }, "name" : "borough_1_ratings_1", "hidden" : true } ]
仅当值为 true 才返回 hidden 索引选项。