Definition定义
planCacheSetFilter- Set an index filter for a collection. If an index filter already exists for the plan cache query shape, the command overrides the previous index filter.
Query Settings
New in version 8.0.在版本8.0中新增。
Starting in MongoDB 8.0, use query settings instead of adding index filters. Index filters are deprecated starting in MongoDB 8.0.
Query settings have more functionality than index filters. Also, index filters aren't persistent and you cannot easily create index filters for all cluster nodes. To add query settings and explore examples, see setQuerySettings.
The query optimizer uses the query settings as an additional input during query planning, which affects the plan selected to run the query.
The settings apply to the query shape on the entire cluster. The cluster retains the settings after shutdown.
You can add query settings for find, distinct, and aggregate commands.
To remove query settings, use removeQuerySettings. To obtain the query settings, use a $querySettings stage in an aggregation pipeline.
Compatibility兼容性
This command is available in deployments hosted in the following environments:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.runCommand(
{
planCacheSetFilter: <collection>,
query: <query>,
sort: <sort>,
projection: <projection>,
collation: { <collation> },
indexes: [ <index1>, <index2>, ...],
comment: <any>
}
)
The plan cache query shape for the index filter is the combination of:索引筛选器的计划缓存查询形状是以下各项的组合:
querysortprojectioncollation
Command Fields命令字段
The command has the following fields:该命令包含以下字段:
planCacheSetFilter | ||
query |
| |
sort | ||
projection | ||
collation | Specifies the collation to use for the operation. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. The collation option has the following syntax:
When specifying collation, the If the collation is unspecified but the collection has a default collation (see If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons. You cannot specify multiple collations for an operation. For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort. Starting in MongoDB 6.0, an index filter uses the collation previously set using the Starting in MongoDB 8.0, use query settings instead of adding index filters. Index filters are deprecated starting in MongoDB 8.0. Query settings have more functionality than index filters. Also, index filters aren't persistent and you cannot easily create index filters for all cluster nodes. To add query settings and explore examples, see | |
indexes | An array of index filters for the specified plan cache query shape. Specify the index filters as one of these arrays:
The query optimizer uses either a collection scan or the index arrays for the query plan. If the specified indexes do not exist or are hidden, the optimizer uses a collection scan. For multiple indexes with the same key pattern, you must specify the index as an array of names. | |
comment |
|
Index filters only exist for the duration of the server process and do not persist after shutdown. To clear the index filters, use the planCacheClearFilters command.
Required Access所需访问权限
A user must have access that includes the planCacheIndexFilter action.
Examples示例
Set Filter on Plan Cache Query Shape Consisting of Predicate Only
The following example creates an index filter on the orders collection such that for queries that consist only of an equality match on the status field without any projection and sort, the query optimizer evaluates only the two specified indexes and the collection scan for the winning plan:
db.runCommand(
{
planCacheSetFilter: "orders",
query: { status: "A" },
indexes: [
{ cust_id: 1, status: 1 },
{ status: 1, order_date: -1 }
]
}
)
In the query predicate, only the structure of the predicate, including the field names, are significant; the values are insignificant. As such, the created filter applies to the following operations:
db.orders.find( { status: "D" } )
db.orders.find( { status: "P" } )
To see whether MongoDB will apply an index filter for a plan cache query shape, check the indexFilterSet field of either the db.collection.explain() or the cursor.explain() method.
Set Filter on Plan Cache Query Shape Consisting of Predicate, Projection, and Sort在由谓词、投影和排序组成的计划缓存查询形状上设置筛选器
The following example creates an index filter for the orders collection. The filter applies to queries whose predicate is an equality match on the item field, where only the quantity field is projected and an ascending sort by order_date is specified.
db.runCommand(
{
planCacheSetFilter: "orders",
query: { item: "ABC" },
projection: { quantity: 1, _id: 0 },
sort: { order_date: 1 },
indexes: [
{ item: 1, order_date: 1 , quantity: 1 }
]
}
)
For the plan cache query shape, the query optimizer will only consider indexed plans which use the index 对于计划缓存查询形状,查询优化器将只考虑使用索引{ item: 1, order_date: 1, quantity: 1 }.{ item: 1, order_date: 1, quantity: 1 }的索引计划。
Set Filter on Plan Cache Query Shape Consisting of Predicate and Collation在由谓词和排序规则组成的计划缓存查询形状上设置筛选器
The following example creates an index filter for the orders collection. The filter applies to queries whose predicate is an equality match on the item field and the collation en_US (English United States).
db.runCommand(
{
planCacheSetFilter: "orders",
query: { item: "Movie" },
collation: { locale: "en_US" },
indexes: [
{ item: 1, order_date: 1 , quantity: 1 }
]
}
)
For the plan cache query shape, the query optimizer only uses indexed plans that use the index { item: 1, order_date: 1, quantity: 1 }.
Starting in MongoDB 6.0, an index filter uses the collation previously set using the planCacheSetFilter command.
Starting in MongoDB 8.0, use query settings instead of adding index filters. Index filters are deprecated starting in MongoDB 8.0.从MongoDB 8.0开始,使用查询设置,而不是添加索引筛选器。索引筛选器从MongoDB 8.0开始就被弃用。
Query settings have more functionality than index filters. Also, index filters aren't persistent and you cannot easily create index filters for all cluster nodes. To add query settings and explore examples, see 查询设置比索引筛选器具有更多功能。此外,索引筛选器不是持久的,您无法轻松为所有集群节点创建索引筛选器。要添加查询设置并探索示例,请参阅setQuerySettings.setQuerySettings。