db.collection.hideIndex()
定义
db.collection.hideIndex()
重要
mongosh 方法
本页面提供
mongosh
方法的相关信息。这不是数据库命令或特定语言驱动程序(例如 Node.js)的相关文档。For the database command, see the
index.hidden
collection option set using thecollMod
command.如需了解 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.
兼容性
此方法可用于以下环境中托管的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
db.collection.hideIndex(<index>)
参数
db.collection.hideIndex()
方法采用以下参数:
Parameter | 类型 | 说明 |
---|---|---|
| 字符串或文档 | 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. 要查找索引名称或索引规范文档,可以使用 To hide a text index, specify the index name. |
是db.collection.hideIndex()
mongosh
shellcollMod
命令的 包装器。
行为
特征兼容性版本
要隐藏索引,必须将 featureCompatibilityVersion 设置为 5.0
或更大。
限制
You cannot hide the _id
index.
索引修改重置统计信息
Hiding an unhidden index resets its $indexStats
.
No-op
Hiding an already hidden index has no effect on the index. However, the operation will still generate an empty oplog entry.
访问控制
如果部署强制执行身份验证/授权,则您必须在集合的数据库中拥有 collMod
权限。
内置角色dbAdmin
提供所需的特权。
例子
The following example hides an existing index.
First, use db.collection.createIndex()
to create an index
without hiding:
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 } ]
The hidden
index option is only returned if the value is true
.
另请参阅: