- Reference >
mongoShell Methods >- Cursor Methods >
- cursor.max()
cursor.max()¶
On this page
Definition¶
-
cursor.max()¶ mongoShell MethodThis page documents the
mongoshell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.Specifies the exclusive upper bound for a specific index in order to constrain the results of
find().max()provides a way to specify an upper bound on compound key indexes.
Parameters¶
The max() method has the following parameter:
| Parameter | Type | Description |
|---|---|---|
indexBounds |
document | The exclusive upper bound for the index keys. |
The indexBounds parameter has the following prototype form:
The fields correspond to all the keys of a particular index in order.
Index Use
Starting in MongoDB 4.2, you must explicitly specify the particular
index with the hint() method to run
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> }.
In previous versions, you could run max() with or
without explicitly hinting the index regardless of the query
condition. If run without the hint in 4.0 and earlier, MongoDB
selects the index using the fields in the indexBounds; however,
if multiple indexes exist on same fields with different sort orders,
the selection of the index may be ambiguous.
See also
max() exists primarily to support the
mongos (sharding) process, and is a shell wrapper around
the query modifier $max.
Deprecated since v3.2
Starting in v3.2, the $max operator is deprecated in the
mongo shell. In the mongo shell,
use cursor.max() instead.
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 will use the index on the price 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.
Example¶
Note
Starting in MongoDB 4.2, you must explicitly specify the particular
index with the hint() method to run
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> }.
For the examples below, create a sample collection named products that holds the
following documents:
Create the following indexes for the collection:
Using the ordering of
{ item: 1, type: 1 }index,max()limits the query to the documents that are below the bound ofitemequal toappleandtypeequal tojonagold:The query returns the following documents:
Using the ordering of the index
{ price: 1 },max()limits the query to the documents that are below the index key bound ofpriceequal toNumberDecimal("1.99")andmin()limits the query to the documents that are at or above the index key bound ofpriceequal toNumberDecimal("1.39"):Note
The query returns the following documents: