Navigation
This version of the documentation is archived and no longer supported.

$min

$min

Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find(). The mongo shell provides the cursor.min() wrapper method:

db.collection.find( { <query> } ).min( { field1: <min value>, ... fieldN: <min valueN>} )

You can also specify the option with either of the two forms:

db.collection.find( { <query> } )._addSpecial( "$min", { field1: <min value1>, ... fieldN: <min valueN> } )
db.collection.find( { $query: { <query> }, $min: { field1: <min value1>, ... fieldN: <min valueN> } } )

The $min specifies the lower bound for all keys of a specific index in order.

Consider the following operations on a collection named collection that has an index { age: 1 }:

db.collection.find().min( { age: 20 } )

These operations limit the query to those documents where the field age is at least 20 using the index { age: 1 }.

You can explicitly specify the corresponding index with cursor.hint(). Otherwise, 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.

Consider a collection named collection that has the following two indexes:

{ age: 1, type: -1 }
{ age: 1, type: 1 }

Without explicitly using cursor.hint(), it is unclear which index the following operation will select:

db.collection.find().min( { age: 20, type: 'C' } )

You can use $min in conjunction with $max to limit results to a specific range for the same index, as in the following example:

db.collection.find().min( { age: 20 } ).max( { age: 25 } )

Note

Because cursor.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:

db.collection.find( { _id: 7 } ).min( { age: 25 } )

The query will use the index on the age field, even if the index on _id may be better.

←   $maxScan $natural  →