Changed in version 8.0.在版本8.0中的更改。
A query shape is a set of specifications that group similar queries together. Specifications can include filters, sorts, projections, aggregation pipeline stages, a namespace, and others. Queries that have similar specifications have the same query shape.查询形状是一组将类似查询组合在一起的规范。规范可以包括筛选器、排序、投影、聚合管道阶段、名称空间等。具有相似规范的查询具有相同的查询形状。
Note
Starting in MongoDB 8.0, the pre-existing query shape is renamed to the plan cache query shape, and the 从MongoDB 8.0开始,将预先存在的查询形状重命名为计划缓存查询形状,并将queryHash field is renamed planCacheShapeHash. queryHash字段重命名为planCacheShapeHash。For the components of a command that differentiate plan cache query shapes, see the plan cache query shape definition. 关于区分计划缓存查询形状的命令组件,请参阅计划缓存查询的形状定义。MongoDB 8.0 uses the new query shape and the existing plan cache query shape as shown on this page.MongoDB 8.0使用新的查询形状和现有的计划缓存查询形状,如本页所示。
In MongoDB 8.0 and earlier versions, the existing plan cache query shape supports:在MongoDB 8.0及更早版本中,现有的计划缓存查询形状支持:
query planning in plan cache scenarios.计划缓存场景中的查询规划。deprecated index filters. (Starting in MongoDB 8.0, use the new query settings for the new query shape.)已弃用索引筛选器。(从MongoDB 8.0开始,使用新查询形状的新查询设置。)a subset of the Aggregation Pipeline.聚合管道的一个子集。
Starting in MongoDB 8.0, the new query shape supports:从MongoDB 8.0开始,新的查询形状支持:
query settings, which you can add with查询设置,您可以使用setQuerySettings. (As you'll see later on this page, query settings specify indexes and execution settings for a query shape.)setQuerySettings添加。(正如您将在本页稍后看到的,查询设置指定了查询形状的索引和执行设置。)$queryStatsquery shape statistics.查询形状统计信息。the fields and operations also supported by the plan cache query shape. Examples:计划缓存查询形状也支持这些字段和操作。示例:filter,sort, andprojection.filter、sort和projection。the majority of the fields and operands available for the可用于find,distinct, andaggregatecommands. To view the fields and operands for the commands, see the Syntax sections on each command page.find、distinct和aggregate命令的大多数字段和操作数。要查看命令的字段和操作数,请参阅每个命令页面上的语法部分。the overall structure of thefind,aggregation, anddistinctcommands, which support a wider range of query shapes than the existing plan cache query shape.find、aggregation和distinct命令的总体结构,支持比现有计划缓存查询形状更广泛的查询形状。operation rejection filters to block操作拒绝筛选器可阻止具有指定查询形状的find,aggregation, anddistinctcommands that have a specified query shape.find、aggregation和distinct命令。the entire aggregation pipeline.整个聚合管道。
Starting in MongoDB 8.0, you can use a 从MongoDB 8.0开始,您可以使用$querySettings pipeline stage to return query settings specified for each query shape.$querySettings管道阶段返回为每个查询形状指定的查询设置。
Examples示例
The following sections show examples with query shapes for the following example 以下部分显示了以下示例pizzaOrders collection:pizzaOrders集合的查询形状示例:
db.pizzaOrders.insertMany( [
{ _id: 0, type: "pepperoni", size: "small", price: 19,
totalNumber: 10, orderDate: ISODate( "2023-03-13T08:14:30Z" ) },
{ _id: 1, type: "pepperoni", size: "medium", price: 20,
totalNumber: 20, orderDate: ISODate( "2023-03-13T09:13:24Z" ) },
{ _id: 2, type: "pepperoni", size: "large", price: 21,
totalNumber: 30, orderDate: ISODate( "2023-03-17T09:22:12Z" ) },
{ _id: 3, type: "cheese", size: "small", price: 12,
totalNumber: 15, orderDate: ISODate( "2023-03-13T11:21:39.736Z" ) }
] )
Matching Query Shapes匹配查询形状
The following example query shape shows specifications for a 以下示例查询形状显示了默认find command on the pizzaOrders collection in the default test database:test数据库中pizzaOrders集合上find命令的规范:
find: "pizzaOrders",
filter: {
orderDate: { $gt: ISODate( "2023-01-20T00:00:00Z" ) }
},
sort: {
totalNumber: 1
},
$db: "test"
The example filter limits the documents to those with an order date greater than the specified date. The example has an ascending sort on the total number of pizzas ordered.示例筛选器将文档限制为订单日期大于指定日期的文档。该示例对订购的披萨总数进行升序排序。
To view the MongoDB 8.0 要查看MongoDB 8.0 queryShapeHash and planCacheShapeHash hexadecimal strings, you can use the explain command.queryShapeHash和planCacheShapeHash十六进制字符串,可以使用explain命令。
The following 以下explain examples contain queries with the same query shape:explain示例包含具有相同查询形状的查询:
db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2024-05-10T05:15:35Z" ) } } ).
sort( { totalNumber: 1 }
)
db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2024-02-05T07:07:16Z" ) } } ).
sort( { totalNumber: 1 }
)
db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2023-03-08T08:12:25Z" ) } } ).
sort( { totalNumber: 1 }
)
Because the query shapes are the same, the 因为查询形状相同,所以explain output has the same queryShapeHash for each of the examples, and the same planCacheShapeHash for each. For example:explain输出对每个示例都有相同的queryShapeHash,对每个示例也有相同的planCacheShapeHash。例如:
queryShapeHash: 'AB8ECADEE8F0EB0F447A30744EB4813AE7E0BFEF523B0870CA10FCBC87F5D8F1'
planCacheShapeHash: '48E51110'Different Query Shape不同的查询形状
A query has a different shape if the query has different specifications. For example, if a query has a different filter, sort, projection, namespace, or aggregation pipeline stages.如果查询具有不同的规范,则查询的形状也会不同。例如,如果查询具有不同的筛选器、排序、投影、名称空间或聚合管道阶段。
In the example in the previous section, the example sorts by the 在上一节的示例中,该示例按totalNumber field. If you change the query sort from totalNumber to a different field, the query has a different query shape.totalNumber字段排序。如果将查询排序从totalNumber更改为其他字段,则查询具有不同的查询形状。
For example, sorting by pizza 例如,按披萨price changes the query shape:price排序会改变查询形状:
db.pizzaOrders.explain().find(
{ orderDate: { $gt: ISODate( "2023-01-20T00:00:00Z" ) } } ).
sort( { price: 1 }
)
Because the query shape is different from the shape in the previous section, the 因为查询形状与上一节中的形状不同,所以explain output has a different queryShapeHash from the previous example, and a different planCacheShapeHash. For example:explain输出的queryShapeHash与上一个示例不同,planCacheShapeHash也不同。例如:
queryShapeHash: 'AC1ECADBE8F1EB0F417A30741AB4813BE7E0BFEF523B0870CA11FCBC87F1A8B2'
planCacheShapeHash: '31A52130'Behavior行为
Starting in MongoDB 8.0, add query settings for query shapes instead of index filters for collections. 从MongoDB 8.0开始,为查询形状添加查询设置,而不是为集合添加索引筛选器。Index filters are deprecated starting in MongoDB 8.0. Query settings have more functionality than index filters, and index filters aren't persistent after cluster shutdown.索引筛选器从MongoDB 8.0开始就被弃用。查询设置比索引筛选器具有更多的功能,并且索引筛选器在集群关闭后不是持久的。
Query settings allow you to use an index for all executions of a query shape in a cluster. Also, to prevent an operation from causing excessive cluster workload, you can reject all operations associated with a query shape using an operation rejection filter.查询设置允许您对集群中查询形状的所有执行使用索引。此外,为了防止操作导致过多的集群工作负载,您可以使用操作拒绝筛选器拒绝与查询形状关联的所有操作。
The query optimizer uses the query settings as an additional input during query planning. The query settings affect the plan selected to run a query that has a matching query shape.查询优化器在查询规划期间使用查询设置作为附加输入。查询设置会影响为运行具有匹配查询形状的查询而选择的计划。
Get Started开始使用
To add query settings for a query shape, use要为查询形状添加查询设置,请使用setQuerySettings.setQuerySettings。To delete query settings, use要删除查询设置,请使用removeQuerySettings.removeQuerySettings。To retrieve query settings, use a要检索查询设置,请在聚合管道中使用$querySettingsstage in an aggregation pipeline.$querySettings阶段。To block a query shape, use an operation rejection filter.要阻止查询形状,请使用操作拒绝筛选器。