$max

On this page本页内容

Definition定义

$max

Deprecated since v3.2从v3开始就不推荐了。2.

Starting in v3.2, the $max operator is deprecated in the mongo shell. 从v3.2开始,$max运算符在mongo shell中不受欢迎。In the mongo shell, use cursor.max() instead.mongo shell中,请改用cursor.max()

Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find(). 指定$max值以指定特定索引的排除式上限,以约束find()的结果。The $max specifies the upper bound for all keys of a specific index in order.$max按顺序指定特定索引的所有键的上限。

The mongo shell provides the max() wrapper method:mongo shell提供了max()包装方法:

db.collection.find( { <query> } ).max( { field1: <max value>, ... fieldN: <max valueN> } )

You can also specify $max with either of the two forms:还可以使用以下两种形式之一指定$max

db.collection.find( { <query> } )._addSpecial( "$max", { field1: <max value1>, ... fieldN: <max valueN> } )
db.collection.find( { $query: { <query> }, $max: { field1: <max value1>, ... fieldN: <max valueN> } } )

Index Use索引使用

Starting in MongoDB 4.2, you must explicitly specify the particular index with the hint() method to run max()/ $max with the following exception: you do not need to hint if the find() query is an equality condition on the _id field { _id: <value> }.从MongoDB 4.2开始,必须使用hint()方法显式指定特定的索引,以运行max()/$max,但有以下例外:如果find()查询是_id字段{ _id: <value> }上的相等条件,则无需提示。

Behavior行为

Interaction with Index Selection与索引选择的互动

Because max() requires an index on a field, and forces the query to use this index, you may prefer the $lt operator for the query if possible. 因为max()需要字段上的索引,并强制查询使用此索引,所以如果可能的话,您可能更喜欢使用$lt运算符进行查询。Consider the following example:考虑下面的例子:

db.collection.find(  { _id: { $in: [ 6, 7 ] } } ).max( { age: 25 } ).hint( { age: 1 } )

The query uses the index on the age field, even if the index on _id may be better.查询使用age字段上的索引,即使_id上的索引可能更好。

Index Bounds索引界限

If you use $max with $min to specify a range:如果使用$max$min指定范围:

  • the index bounds specified in $min and $max must both refer to the keys of the same index.$min$max中指定的索引边界必须同时引用同一索引的键。
  • the bound specified by $max must be greater than the bound specified by $min.$max指定的界限必须大于$min指定的界限。

    Changed in version 4.0.在版本4.0中更改。

$max without 不用$min

The min and max operators indicate that the system should avoid normal query planning. minmax运算符表示系统应避免正常的查询计划。Instead they construct an index scan where the index bounds are explicitly specified by the values given in min and max.相反,它们构造一个索引扫描,其中索引边界由minmax中给出的值显式指定。

Warning警告

If one of the two boundaries is not specified, the query plan will be an index scan that is unbounded on one side. 如果未指定两个边界中的一个,则查询计划将是一侧无边界的索引扫描。This may degrade performance compared to a query containing neither operator, or one that uses both operators to more tightly constrain the index scan.与既不包含运算符,也不使用两个运算符来更严格地约束索引扫描的查询相比,这可能会降低性能。

Examples示例

The following examples use the mongo shell wrappers.以下示例使用mongo shell包装器。

Specify Exclusive Upper Bound指定独占上限

Consider the following operations on a collection named collection that has an index { age: 1 }:考虑一个名为collection的集合的以下操作,该集合具有索引{ age: 1 }

db.collection.find( { <query> } ).max( { age: 100 } ).hint( { age: 1 } )

This operation limits the query to those documents where the field age is less than 100 and forces a query plan which scans the { age: 1 } index from MinKey to 100.此操作将查询限制为字段age小于100的文档,并强制执行一个查询计划,将{ age: 1 }索引从MinKey扫描到100

Use with $min$min一起使用

Use $max alone or in conjunction with $min to limit results to a specific range for the same index, as in the following example:单独使用$max或与$min结合使用,将同一索引的结果限制在特定范围内,如下例所示:

Note

Changed in version 4.0:在版本4.0中更改:The bound specified by $max must be greater than the bound specified by $min.$max指定的界限必须大于$min指定的界限。

db.collection.find().min( { age: 20 } ).max( { age: 25 } ).hint( { age: 1 } )