对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db. 集合.unhideIndex()(mongosh方法)

重要

mongosh 方法

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

有关该数据库命令,请参阅使用 collMod 命令设置的 index.hidden 集合选项。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

db.collection.unhideIndex()

从查询规划器中取消隐藏现有索引。 取消隐藏后,索引就可以立即使用。

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

db.collection.unhideIndex(<index>)

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

Parameter
类型
说明

index

字符串或文档

指定要在查询规划器中取消隐藏的索引。 您可以通过索引名称或索引规范文档来指定索引。

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

要取消隐藏文本索引,请指定索引名称。

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

取消隐藏隐藏索引会重置其$indexStats

取消隐藏已取消隐藏的索引对该索引没有影响。 但是,该操作仍会生成空的 oplog 条目。

如果部署强制执行身份验证/授权,则您必须在集合的数据库中拥有 collMod 权限。

内置角色dbAdmin提供所需的特权。

以下示例取消隐藏现有索引。

首先,使用db.collection.createIndex()创建隐藏索引:

db.restaurants.createIndex( { borough: 1, ratings: 1 }, { hidden: true } );

要进行验证,请在 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(隐藏)。

要取消隐藏索引,您可以为db.collection.unhideIndex()方法指定索引键规范文档或索引名称。 指定索引名称如下:

db.restaurants.unhideIndex( "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 不再显示为 borough_1_ratings_1 索引的一部分,因为仅当值为 true 时才返回该字段。