- Reference >
- Operators >
- Query Modifiers >
- $max
$max¶
-
$max¶ Specify a
$maxvalue to specify the exclusive upper bound for a specific index in order to constrain the results offind(). Themongoshell provides themax()wrapper method:You can also specify the option with either of the two forms:
The
$maxspecifies the upper bound for all keys of a specific index in order.Consider the following operations on a collection named
collectionthat has an index{ age: 1 }:This operation limits the query to those documents where the field
ageis less than100using the index{ age: 1 }.You can explicitly specify the corresponding index with
hint(). Otherwise, MongoDB selects the index using the fields in theindexBounds; however, if multiple indexes exist on same fields with different sort orders, the selection of the index may be ambiguous.Consider a collection named
collectionthat has the following two indexes:Without explicitly using
hint(), MongoDB may select either index for the following operation:Use
$maxalone or in conjunction with$minto limit results to a specific range for the same index, as in the following example: