“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

db.collection.unhideIndex()

在此页面上

  • 定义
  • 语法
  • 行为
  • 必需的访问权限
  • 例子
db.collection.unhideIndex()

重要

mongosh 方法

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

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

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

对于传统 mongo Shell 文档,请参阅相应 MongoDB Server 版本的文档:

mongo shell v4.4

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

db.collection.unhideIndex(<index>)

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

参数
类型
说明
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 时才返回该字段。

提示

另请参阅:

← db.collection.totalSize()