Docs 菜单

db.collection.hideIndex()

db.collection.hideIndex()

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是数据库命令或特定语言驱动程序(例如 Node.js)的相关文档。

For the database command, see the index.hidden collection option set using the collMod 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 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

db.collection.hideIndex(<index>)

db.collection.hideIndex()方法采用以下参数:

Parameter
类型
说明

index

字符串或文档

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.

要查找索引名称或索引规范文档,可以使用db.collection.getIndexes()方法。

To hide a text index, specify the index name.

db.collection.hideIndex() mongoshshellcollMod命令的 包装器。

要隐藏索引,必须将 featureCompatibilityVersion 设置为 5.0 或更大。

You cannot hide the _id index.

Hiding an unhidden index resets its $indexStats.

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.

另请参阅: