cursor.max()

Definition

  • cursor.max()

mongo Shell Method

This page documents the mongo shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.

Specifies the exclusive upper bound for a specific index in orderto constrain the results offind(). max() provides away to specify an upper bound on compound key indexes.

Parameters

The max() method has the following parameter:

ParameterTypeDescription
indexBoundsdocumentThe exclusive upper bound for the index keys.

The indexBounds parameter has the following prototype form:

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

The fields correspond to all the keys of a particular index inorder.

See also

min().

max() exists primarily to support themongos (sharding) process, and is a shell wrapper aroundthe query modifier $max.

Deprecated since v3.2

Starting in v3.2, the $max operator is deprecated in themongo shell. In the mongo shell,use cursor.max() instead.

Behavior

Interaction with Index Selection

Because max() requires an index on a field,and forces the query to use this index, you may prefer the$lt operator for the query if possible. Consider thefollowing example:

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

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

Index Bounds

If you use max() with min() tospecify a range:

  • the index bounds specified in min() andmax() must both refer to the keys of the same index.

  • the bound specified by max() must be greater thanthe bound specified by min().

Changed in version 4.0.

max() without min()

The min and max operators indicate that the systemshould avoid normal query planning. Instead they construct an index scan wherethe index bounds are explicitly specified by the values given inmin and max.

Warning

If one of the two boundaries is not specified, the query plan will bean index scan that is unbounded on one side. This may degrade performancecompared to a query containing neither operator, or one that uses bothoperators to more tightly constrain the index scan.

Example

Note

Starting in MongoDB 4.2, you must explicitly specify the particularindex with the hint() method to runmax() with the following exception: you do notneed to hint if the find() query is anequality condition on the _id field { _id: <value> }.

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

  1. db.products.insertMany([
  2. { "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : NumberDecimal("1.99") },
  3. { "_id" : 2, "item" : "apple", "type" : "fuji", "price" : NumberDecimal("1.99") },
  4. { "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : NumberDecimal("1.29") },
  5. { "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : NumberDecimal("1.29") },
  6. { "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : NumberDecimal("1.29") },
  7. { "_id" : 6, "item" : "apple", "type" : "cortland", "price" : NumberDecimal("1.29") },
  8. { "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : NumberDecimal("2.99") },
  9. { "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : NumberDecimal("1.99") },
  10. { "_id" : 8, "item" : "orange", "type" : "valencia", "price" : NumberDecimal("0.99") },
  11. { "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") }
  12. ])

Create the following indexes for the collection:

  1. db.products.createIndexes( [
  2. { "item" : 1, "type" : 1 },
  3. { "item" : 1, "type" : -1 },
  4. { "price" : 1 }
  5. ] )
  • Using the ordering of { item: 1, type: 1 } index,max() limits the query to the documentsthat are below the bound of item equal to apple andtype equal to jonagold:
  1. db.products.find().max( { item: 'apple', type: 'jonagold' } ).hint( { item: 1, type: 1 } )

The query returns the following documents:

  1. { "_id" : 6, "item" : "apple", "type" : "cortland", "price" : NumberDecimal("1.29") }
  2. { "_id" : 2, "item" : "apple", "type" : "fuji", "price" : NumberDecimal("1.99") }
  3. { "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : NumberDecimal("1.99") }
  • Using the ordering of the index { price: 1 }, max()limits the query to the documents that are belowthe index key bound of price equal to NumberDecimal("1.99") andmin() limits the query to the documentsthat are at or above the index key bound of price equal toNumberDecimal("1.39"):

Note

Changed in version 4.0: The bound specified by max() must be greaterthan the bound specified by min().

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

The query returns the following documents:

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