Hidden indexes are not visible to the query planner and cannot be used to support a query.隐藏索引对查询计划器不可见,不能用于支持查询。
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.通过对计划器隐藏索引,您可以在不实际删除索引的情况下评估删除索引的潜在影响。如果影响是负面的,您可以取消隐藏索引,而不必重新创建已删除的索引。
Behavior行为
Apart from being hidden from the planner, hidden indexes behave like unhidden indexes. For example:除了对计划器隐藏之外,隐藏索引的行为类似于未隐藏索引。例如:
If a hidden index is a unique index, the index still applies its unique constraint to the documents.如果隐藏索引是唯一索引,则该索引仍将其唯一约束应用于文档。If a hidden index is a TTL index, the index still expires documents.如果隐藏索引是TTL索引,则该索引仍然会使文档过期。Hidden indexes are included in隐藏索引包含在listIndexesanddb.collection.getIndexes()results.listIndexes和db.collection.getIndexes()结果中。Hidden indexes are updated upon write operations to the collection and continue to consume disk space and memory. As such, they are included in various statistics operations, such as隐藏索引在对集合进行写入操作时更新,并继续消耗磁盘空间和内存。因此,它们被包含在各种统计操作中,如db.collection.stats()and$indexStats.db.collection.stats()和$indexStats。Hiding an unhidden index or unhiding a hidden index resets its隐藏未隐藏的索引或取消隐藏隐藏的索引会重置其$indexStats.$indexStats。Hiding an already hidden index or unhiding an already unhidden index does not reset the隐藏已隐藏的索引或取消隐藏已取消隐藏的索引不会重置$indexStats.$indexStats。
Restrictions限制
To hide an index, you must have featureCompatibilityVersion set to要隐藏索引,必须将6.0or greater.featureCompatibilityVersion设置为6.0或更高版本。You cannot hide the您无法隐藏_idindex._id索引。You cannot您不能用cursor.hint()a hidden index.cursor.hint()隐藏索引。
Examples示例
Create a Hidden Index创建隐藏索引
To create a 要创建隐藏索引,请使用hidden index, use the db.collection.createIndex() method with the hidden option set to true.db.collection.createIndex()方法,并将hidden选项设置为true。
Note
To use the 要在hidden option with db.collection.createIndex(), you must have featureCompatibilityVersion set to 6.0 or greater.db.collection.createIndex()中使用hidden选项,您必须将featureCompatibilityVersion设置为6.0或更高版本。
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。
Hide an Existing Index隐藏现有索引
Note
To hide an index, you must have featureCompatibilityVersion set to要隐藏索引,必须将6.0or greater.featureCompatibilityVersion设置为6.0或更高版本。You cannot hide the您无法隐藏_idindex._id索引。
To hide an existing index, you can use the 要隐藏现有索引,可以使用collMod command or mongosh helper db.collection.hideIndex().collMod命令或mongosh助手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:要隐藏索引,您可以指定:
the index key specification document to the将索引键规范文档转换为db.collection.hideIndex()method:db.collection.hideIndex()方法:db.restaurants.hideIndex( { borough: 1, ratings: 1 } ); //Specify the index key specification document指定索引键规范文档the index name to thedb.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时,才会返回hidden的索引选项。
Unhide an Existing Index取消隐藏现有索引
To unhide a hidden index, you can use the 要取消隐藏隐藏的索引,可以使用collMod command or mongosh helper db.collection.unhideIndex(). You can specify either:collMod命令或mongosh辅助程序db.collection.unhideIndex()。您可以指定:
the index key specification document to the将索引键规范文档转换为db.collection.unhideIndex()method:db.collection.unhideIndex()方法:db.restaurants.unhideIndex( { borough: 1, city: 1 } ); //Specify the index key specification document指定索引键规范文档the index name to thedb.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.因为索引在隐藏时是完全维护的,所以一旦取消隐藏,索引就可以立即使用。