$min

On this page本页内容

Definition定义

$min

Deprecated since v3.2

Starting in v3.2, the $min operator is deprecated in the mongo shell. In the mongo shell, use cursor.min() instead.

Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find(). The $min specifies the lower bound for all keys of a specific index in order.

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

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

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

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

Index Use

Starting in MongoDB 4.2, you must explicitly specify the particular index with the hint() method to run min()/ $min with the following exception: you do not need to hint if the find() query is an equality condition on the _id field { _id: <value> }.

Behavior行为

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

Because min() requires an index on a field, and forces the query to use this index, you may prefer the $gte operator for the query if possible. Consider the following example:

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

The query will use 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,

  • the index bounds specified in $min and $max must both refer to the keys of the same index.
  • the bound specified by $max must be greater than the bound specified by $min.

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

$min without $max

The min and max operators indicate that the system should avoid normal query planning. Instead they construct an index scan where the index bounds are explicitly specified by the values given in min and 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.与既不包含运算符,也不使用两个运算符来更严格地约束索引扫描的查询相比,这可能会降低性能。

Examples示例

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

Specify Inclusive Lower Bound指定包含下限

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

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

This operation limits the query to those documents where the field age is at least 20 and forces a query plan which scans the { age: 1 } index from 20 to MaxKey.

Use with $max

You can use $min in conjunction with $max to limit results to a specific range for the same index, as in the following example:

Note

Changed in version 4.0:The bound specified by $max must be greater than the bound specified by $min.

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