Database Manual / Reference / mongosh Methods / Query Plan Caches

PlanCache.clearPlansByQuery() (mongosh method方法)

Definition定义

PlanCache.clearPlansByQuery( <query>, <projection>, <sort> )

Clears the cached query plans for the specified plan cache query shape.清除指定计划缓存查询形状的缓存查询计划。

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.本页记录了一种mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。

For the database command, see the planCacheClear command.关于数据库命令,请参阅planCacheClear命令。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

The method is only available from the plan cache object of a specific collection; i.e.该方法仅在特定集合的计划缓存对象中可用;即。

db.collection.getPlanCache().clearPlansByQuery( <query>, <projection>, <sort> )

The PlanCache.clearPlansByQuery() method accepts the following parameters:PlanCache.clearPlansByQuery()方法接受以下参数:

Parameter参数Type类型Description描述
querydocument文档The query predicate of the plan cache query shape. Only the structure of the predicate, including the field names, are significant to the shape; the values in the query predicate are insignificant.计划缓存查询形状的查询谓词。只有谓词的结构,包括字段名,对形状有意义;查询谓词中的值是不重要的。
projectiondocument文档Optional. 可选。The projection associated with the plan cache query shape. Required if specifying the sort parameter.与计划缓存查询形状关联的投影。如果指定sort参数,则为必填项。
sortdocument文档Optional. 可选。The sort associated with the plan cache query shape.与计划缓存查询形状关联的排序。

To see the query shapes for which cached query plans exist, see Examples.要查看存在缓存查询计划的查询形状,请参阅示例

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

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • 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的源代码可用、免费使用和自我管理版本

Required Access所需访问权限

On systems running with authorization, a user must have access that includes the planCacheWrite action.authorization运行的系统上,用户必须具有包含planCacheWrite操作的访问权限。

Example示例

If a collection orders has the following plan cache query shape:如果集合orders具有以下计划缓存查询形状:

{
"query" : { "qty" : { "$gt" : 10 } },
"sort" : { "ord_date" : 1 },
"projection" : { },
"planCacheShapeHash" : "9AAD95BE"
}

Warning

Starting in MongoDB 8.0, the existing queryHash field is duplicated in a new field named planCacheShapeHash. 从MongoDB 8.0开始,现有的queryHash字段被复制到一个名为planCacheShapeHash的新字段中。If you're using an earlier MongoDB version, you'll only see the queryHash field. 如果你使用的是早期的MongoDB版本,你只会看到queryHash字段。Future MongoDB versions will remove the deprecated queryHash field, and you'll need to use the planCacheShapeHash field instead.未来的MongoDB版本将删除弃用的queryHash字段,您将需要使用planCacheShapeHash字段。

The following operation removes the query plan cached for the shape:以下操作将删除为形状缓存的查询计划:

db.orders.getPlanCache().clearPlansByQuery(
{ "qty" : { "$gt" : 10 } },
{ },
{ "ord_date" : 1 }
)

Tip