- Reference >
- Operators >
- Query Modifiers >
- $max
$max¶
On this page
Definition¶
-
$max
¶ Deprecated since v3.2
Starting in v3.2, the
$max
operator is deprecated in themongo
shell. In themongo
shell, usecursor.max()
instead.Specify a
$max
value to specify the exclusive upper bound for a specific index in order to constrain the results offind()
. The$max
specifies the upper bound for all keys of a specific index in order.The
mongo
shell provides themax()
wrapper method:You can also specify
$max
with either of the two forms:
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. Consider the
following example:
The query uses the index on the age
field, even if the
index on _id
may be better.
Index Bounds¶
$max
without $min
¶
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.