$min¶
-
$min¶ Specify a
$minvalue to specify the inclusive lower bound for a specific index in order to constrain the results offind(). Themongoshell provides thecursor.min()wrapper method:You can also specify the option with either of the two forms:
The
$minspecifies the lower bound for all keys of a specific index in order.Consider the following operations on a collection named
collectionthat has an index{ age: 1 }:These operations limit the query to those documents where the field
ageis at least20using the index{ age: 1 }.You can explicitly specify the corresponding index with
cursor.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
cursor.hint(), it is unclear which index the following operation will select:You can use
$minin conjunction with$maxto limit results to a specific range for the same index, as in the following example:Note
Because
cursor.min()requires an index on a field, and forces the query to use this index, you may prefer the$gteoperator for the query if possible. Consider the following example:The query will use the index on the
agefield, even if the index on_idmay be better.