w3resource

MongoDB: cursor.max() method

cursor.max

The cursor.max() method is used to specify the exclusiveupper bound for a specific index in order to constrain the results offind(). The max()provides a way to specify an upper bound on compound key indexes.

Syntax:

cursor.max()

Parameters:

Name Description Required /
Optional
Type
indexBounds The exclusive upper bound for the index keys. Required document

The indexBounds parameter has the following prototype form:

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

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.max() method

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

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

Output:

> db.prod_mast.find().max( { item: 'mango', type: 'jonagold' } ).hint( { item: 1, type: 1 } );
{ "_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 }

Retrieve the restaurants data from here

Previous: cursor.maxTimeMS() method
Next: cursor.min() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/shell-methods/cursor/cursor-max.php