cursor.min() (original) (raw)

cursor.min()

Important

mongosh Method

This 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-specificMongoDB driver documentation.

Specifies the inclusive lower bound for a specific index in order to constrain the results offind(). 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:


{ field1: <min value>, field2: <min value2>, fieldN:<min valueN> }

Note

Index Use

To use an index with the max() method, you must use thehint() method to specify the index you want to use, except when the find() query is an equality condition on the _id field.

See also:

min() exists primarily to support themongos process.

This method is available in deployments hosted in the following environments:

Note

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, seeUnsupported Commands.

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:


db.products.find( { $in: [ 6, 7 ] } ).min( { price: NumberDecimal("1.39") } ).hint( { price: 1 })

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

If you use min() with max() to specify a range:

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() andmax().

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.

Unless the find() query is an equality condition on the _id field { _id: <value> }, you must explicitly specify the index with the hint() method to run min().

For the examples below, create a sample collection named products that holds the following documents:


db.products.insertMany([

   { "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : NumberDecimal("1.99") },

   { "_id" : 2, "item" : "apple", "type" : "fuji", "price" : NumberDecimal("1.99") },

   { "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : NumberDecimal("1.29") },

   { "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : NumberDecimal("1.29") },

   { "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : NumberDecimal("1.29") },

   { "_id" : 6, "item" : "apple", "type" : "cortland", "price" : NumberDecimal("1.29") },

   { "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : NumberDecimal("2.99") },

   { "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : NumberDecimal("1.99") },

   { "_id" : 8, "item" : "orange", "type" : "valencia", "price" : NumberDecimal("0.99") },

   { "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") }

])

Create the following indexes for the collection:


db.products.createIndexes( [

   { "item" : 1, "type" : 1 },

   { "item" : 1, "type" : -1 },

   { "price" : 1 }

] )

db.products.find().min( { item: 'apple', type: 'jonagold' } ).hint( { item: 1, type: 1 } )  

The query returns the following documents:

{ "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : NumberDecimal("1.29") }  
{ "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : NumberDecimal("1.29") }  
{ "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : NumberDecimal("1.29") }  
{ "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : NumberDecimal("2.99") }  
{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") }  
{ "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : NumberDecimal("1.99") }  
{ "_id" : 8, "item" : "orange", "type" : "valencia", "price" : NumberDecimal("0.99") }  

Note

The bound specified by max() must be greater than the bound specified by min().

db.products.find().min( { price: NumberDecimal("1.39") } ).max( { price:  NumberDecimal("1.99") } ).hint( { price: 1 } )  

The query returns the following documents:

{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") }