w3resource

MongoDB: cursor.min() method

cursor.min

The cursor.min() method is used to specify an inclusive lower bound for a specific index in order to constrain the results of find(). This method provides a way to specify lower bounds on compound key indexes.

Syntax:

cursor.min()

Parameters:

Name Description Required /
Optional
Type
indexBounds The inclusive lower bound for the index keys. Required document

The indexBounds parameter has the following prototype form:

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

The fields correspond to all the keys of a particular index in order. You can explicitly specify the particular index with the hint() method. 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.

min() exists primarily to support the mongos process, and is a shell wrapper around the query modifier $min.

Sample document in the prod_mast collection:


> db.prod_mast.find().pretty();
{ "_id" : 5, "item" : "mango", "type" : "cortland", "cost" : 1.29 }
{ "_id" : 9, "item" : "mango", "type" : "fuji", "cost" : 1.99 }
{ "_id" : 7, "item" : "mango", "type" : "honey crisp", "cost" : 1.99 }
{ "_id" : 10, "item" : "mango", "type" : "jonagold", "cost" : 1.29 }
{ "_id" : 1, "item" : "mango", "type" : "jonathan", "cost" : 1.29 }
{ "_id" : 6, "item" : "mango", "type" : "mcintosh", "cost" : 1.29 }
{ "_id" : 8, "item" : "orange", "type" : "cara cara", "cost" : 2.99 }
{ "_id" : 4, "item" : "orange", "type" : "navel", "cost" : 1.39 }
{ "_id" : 3, "item" : "orange", "type" : "satsuma", "pcost" : 1.99 }
{ "_id" : 2, "item" : "orange", "type" : "valencia", "cost" : 0.99 }

The collection has the following indexes:

>  db.prod_mast.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.prod_mast"
        },
        {
                "v" : 1,
                "key" : {
                        "item" : 1,
                        "type" : 1
                },
                "name" : "item_1_type_1",
                "ns" : "test.prod_mast"
        },
        {
                "v" : 1,
                "key" : {
                        "item" : 1,
                        "type" : -1
                },
                "name" : "item_1_type_-1",
                "ns" : "test.prod_mast"
        },
        {
                "v" : 1,
                "key" : {
                        "cost" : 1
                },
                "name" : "cost_1",
                "ns" : "test.prod_mast"
        }
]

Example: MongoDB: cursor.min() method

The following example return the documents using the ordering of{item:1,type:1}index,min()limits to the documents that are below the bound ofitemequal tomangoandtypeequal tojonagold.

db.prod_mast.find().min( { item: 'mango', type: 'jonagold' } ).hint( { item: 1, type: 1 } );

Output:

> db.prod_mast.find().min( { item: 'mango', type: 'jonagold' } ).hint( { item: 1, type: 1 } );
{ "_id" : 10, "item" : "mango", "type" : "jonagold", "cost" : 1.29 }
{ "_id" : 1, "item" : "mango", "type" : "jonathan", "cost" : 1.29 }
{ "_id" : 6, "item" : "mango", "type" : "mcintosh", "cost" : 1.29 }
{ "_id" : 8, "item" : "orange", "type" : "cara cara", "cost" : 2.99 }
{ "_id" : 4, "item" : "orange", "type" : "navel", "cost" : 1.39 }
{ "_id" : 3, "item" : "orange", "type" : "satsuma", "pcost" : 1.99 }
{ "_id" : 2, "item" : "orange", "type" : "valencia", "cost" : 0.99 }

Retrieve the restaurants data from here

Previous: cursor.max() method
Next: cursor.next() method



Follow us on Facebook and Twitter for latest update.