On this page本页内容
$max¶Deprecated since v3.2从v3开始就不推荐了。2.
Starting in v3.2, the 从v3.2开始,$max operator is deprecated in the mongo shell. $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()包装方法:
You can also specify 还可以使用以下两种形式之一指定$max with either of the two forms:$max:
Index Use索引使用
Starting in MongoDB 4.2, you must explicitly specify the particular index with the 从MongoDB 4.2开始,必须使用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> }.hint()方法显式指定特定的索引,以运行max()/$max,但有以下例外:如果find()查询是_id字段{ _id: <value> }上的相等条件,则无需提示。
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:考虑下面的例子:
The query uses the index on the 查询使用age field, even if the index on _id may be better.age字段上的索引,即使_id上的索引可能更好。
$max$min¶The min and max operators indicate that the system should avoid normal query planning. min和max运算符表示系统应避免正常的查询计划。Instead they construct an index scan where the index bounds are explicitly specified by the values given in 相反,它们构造一个索引扫描,其中索引边界由min and max.min和max中给出的值显式指定。
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.与既不包含运算符,也不使用两个运算符来更严格地约束索引扫描的查询相比,这可能会降低性能。
The following examples use the 以下示例使用mongo shell wrappers.mongo shell包装器。
Consider the following operations on a collection named 考虑一个名为collection that has an index { age: 1 }:collection的集合的以下操作,该集合具有索引{ 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。