- Reference >
mongosh
Methods >- Cursor Methods >
- cursor.min()
cursor.min()¶
On this page
Definition¶
-
cursor.
min
()¶ Important
mongosh
MethodThis page documents a
mongosh
method. This is not the documentation for a language-specific driver such as Node.js.For MongoDB API drivers, refer to the language-specific :driver:`MongoDB driver documentation </>`.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Specifies the inclusive lower bound for a specific index in order to constrain the results of
find()
.min()
provides a way to specify lower bounds on compound key indexes.The
min()
method has the following parameter:Parameter Type Description indexBounds
document The inclusive lower bound for the index keys. The
indexBounds
parameter has the following prototype form:
Note
Index Use
Starting in MongoDB 4.2, you must explicitly specify the particular
index with the hint()
method to run
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> }
.
In previous versions, you could run min()
with or
without explicitly hinting the index. 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
min()
exists primarily to support the
mongos
process.
Behaviors¶
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:
The query will use the index on the price
field, even if
the index on _id
may be better.
Index Bounds¶
min()
without max()
¶
The min()
and max()
methods
indicate that the system should avoid normal query planning. 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¶
Starting in MongoDB 4.2, you must explicitly specify the particular
index with the hint()
method to run
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> }
.
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 the
{ item: 1, type: 1 }
index,min()
limits the query to the documents that are at or above the index key bound ofitem
equal toapple
andtype
equal tojonagold
, as in the following:The query returns the following documents:
Using the ordering of the index
{ price: 1 }
,min()
limits the query to the documents that are at or above the index key bound ofprice
equal to1.39
andmax()
limits the query to the documents that are below the index key bound ofprice
equal to1.99
:Note
Starting in MongoDB 4.0, the bound specified by
max()
must be greater than the bound specified bymin()
.The query returns the following documents: