On this page本页内容
New in version 4.4.版本4.4中的新功能。
Hidden indexes are not visible to the query planner and cannot be used to support a query.隐藏索引对查询计划器不可见,不能用于支持查询。
By hiding an index from the planner, users can evaluate the potential impact of dropping an index without actually dropping the index. 通过对规划器隐藏索引,用户可以在不实际删除索引的情况下评估删除索引的潜在影响。If the impact is negative, the user can unhide the index instead of having to recreate a dropped index.如果影响是负面的,用户可以取消隐藏索引,而不必重新创建已删除的索引。
Apart from being hidden from the planner, hidden indexes behave like unhidden indexes; i.e.隐藏的索引除了对计划器隐藏之外,其行为与未隐藏的索引类似;比如。
listIndexes
and db.collection.getIndexes()
results.listIndexes
和db.collection.getIndexes()
结果中。db.collection.stats()
and $indexStats
.db.collection.stats()
和$indexStats
。$indexStats
. $indexStats
。$indexStats
.$indexStats
。4.4
or greater. 4.4
或更高。4.2
on MongoDB 4.4 binaries.4.2
,索引仍然隐藏。_id
index._id
索引。cursor.hint()
a hidden index.cursor.hint()
隐藏索引。To create a 要创建hidden
index, use the db.collection.createIndex()
method with the hidden option set to true
.hidden
索引,请使用db.collection.createIndex()
方法,并将hidden
选项设置为true
。
Note
To use the 要在hidden
option with db.collection.createIndex()
, you must have featureCompatibilityVersion set to 4.4
or greater. db.collection.createIndex()
中使用hidden
选项,必须将featureCompatibilityVersion设置为4.4
或更高。However, once hidden, the index remains hidden even with featureCompatibilityVersion set to 然而,一旦隐藏,即使在MongoDB 4.4二进制文件上featureCompatibilityVersion设置为4.2
on MongoDB 4.4 binaries.4.2
,索引仍然隐藏。
For example, the following operation creates a hidden ascending index on the 例如,以下操作会在borough
field:borough
字段上创建一个隐藏的升序索引:
db.addresses.createIndex(
{ borough: 1 },
{ hidden: true }
);
To verify, run 要进行验证,请在db.collection.getIndexes()
on the addresses
collection:addresses
集合上运行db.collection.getIndexes()
:
db.addresses.getIndexes()
The operation returns the following information:该操作返回以下信息:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"borough" : 1
},
"name" : "borough_1",
"hidden" : true }
]
The index option 只有当值为hidden
is only returned if the value is true
.true
时,才会返回索引选项hidden
。
Note
4.4
or greater. 4.4
或更高。4.2
on MongoDB 4.4 binaries.4.2
,索引仍然隐藏。_id
index._id
索引。To hide an existing index, you can use the 要隐藏现有索引,可以使用collMod
command or the mongo
shell helper db.collection.hideIndex()
.collMod
命令或mongo
shell助手db.collection.hideIndex()
。
For example, create an index without hiding:例如,在不隐藏的情况下创建索引:
db.restaurants.createIndex( { borough: 1, ratings: 1 } );
To hide the index, you can specify either:要隐藏索引,可以指定:
db.collection.hideIndex()
method:db.collection.hideIndex()
方法的索引键规范文档:
db.restaurants.hideIndex( { borough: 1, ratings: 1 } ); // Specify the index key specification document
db.collection.hideIndex()
method:db.collection.hideIndex()
方法的索引名:
db.restaurants.hideIndex( "borough_1_ratings_1" ); // Specify the index name
To verify, run 要进行验证,请在db.collection.getIndexes()
on the restaurants
collection:restaurants
集合上运行db.collection.getIndexes()
:
db.restaurants.getIndexes()
The operation returns the following information:该操作返回以下信息:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"borough" : 1,
"ratings" : 1
},
"name" : "borough_1_ratings_1",
"hidden" : true }
]
The index option 只有当值为hidden
is only returned if the value is true
.true
时,才会返回index
选项hidden
。
To unhide a hidden index, you can use the 要取消隐藏隐藏的索引,可以使用collMod
command or the mongo
shell helper db.collection.unhideIndex()
. collMod
命令或mongo
shell帮助程序db.collection.unhideIndex()
。You can specify either:您可以指定:
db.collection.unhideIndex()
method:db.collection.unhideIndex()
方法的索引键规范文档:
db.restaurants.unhideIndex( { borough: 1, city: 1 } ); // Specify the index key specification document
db.collection.unhideIndex()
method:db.collection.unhideIndex()
方法的索引名:
db.restaurants.unhideIndex( "borough_1_ratings_1" ); // Specify the index name
To verify, run 要进行验证,请在db.collection.getIndexes()
on the restaurants
collection:restaurants
集合上运行db.collection.getIndexes()
:
db.restaurants.getIndexes()
The operation returns the following information:该操作返回以下信息:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"borough" : 1,
"ratings" : 1
},
"name" : "borough_1_ratings_1"
}
]
The index option 索引选项hidden
no longer appears as part of the borough_1_ratings_1
index since the field is only returned if the value is true
.hidden
不再作为borough_1_ratings_1
索引的一部分出现,因为该字段仅在值为true
时返回。
Because indexes are fully maintained while hidden, the index is immediately available for use once unhidden.因为索引在隐藏时会得到完全维护,所以一旦取消隐藏,索引就可以立即使用。