> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# cursor.max() (mongosh method)

## Definition

**Important: mongosh Method**

This page documents a [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) method. This is *not* the documentation for a language-specific driver, such as Node.js.

For MongoDB API drivers, refer to the language-specific [MongoDB driver documentation.](https://www.mongodb.com/docs/drivers/)

Specifies the *exclusive* upper bound for a specific index in order to constrain the results of [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find). [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) provides a way to specify an upper bound on compound key indexes.

### Parameters

The [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) method has the following parameter:

| Parameter | Type | Description |
| --- | --- | --- |
| `indexBounds` | document | The exclusive upper bound for the index keys. |

The `indexBounds` parameter has the following prototype form:

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

The fields correspond to *all* the keys of a particular index *in
order*.

**Note: Index Use**

To use an index with the [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) method, you must use the [`hint()`](https://www.mongodb.com/docs/manual/reference/method/cursor.hint.md#mongodb-method-cursor.hint) method to specify the index you want to use, except when the [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) query is an equality condition on the `_id` field.

**See also:**

[`min()`](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min)

[`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) exists primarily to support the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) (sharding) process.

## Compatibility

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

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

**Note:**

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see [Unsupported Commands.](https://www.mongodb.com/docs/atlas/unsupported-commands/)

- [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

- [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Behavior

### Interaction with Index Selection

Because [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) requires an index on a field, and forces the query to use this index, you may prefer the [`$lt`](https://www.mongodb.com/docs/manual/reference/operator/query/lt.md#mongodb-query-op.-lt) operator for the query if possible. Consider the following example:

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

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

### Index Bounds

If you use [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) with [`min()`](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min) to specify a range:

- the index bounds specified in [`min()`](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min) and [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) must both refer to the keys of the same index.

- the bound specified by [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) must be greater than the bound specified by [`min()`.](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min)

### `max()` without `min()`

The [`min()`](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min) and [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.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()`](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min) and [`max()`.](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max)

**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.

## Example

**Note:**

You must explicitly specify the particular index with the [`hint()`](https://www.mongodb.com/docs/manual/reference/method/cursor.hint.md#mongodb-method-cursor.hint) method to run [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) with the following exception: you do not need to hint if the [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) query is an equality condition on the `_id` field `{ _id: <value> }`.

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

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

Create the following indexes for the collection:

```javascript
db.products.createIndexes( [
   { "item" : 1, "type" : 1 },
   { "item" : 1, "type" : -1 },
   { "price" : 1 }
] )
```

- Using the ordering of `{ item: 1, type: 1 }` index, [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) limits the query to the documents that are below the bound of `item` equal to `apple` and `type` equal to `jonagold`:

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

  The query returns the following documents:

  ```javascript
  { "_id" : 6, "item" : "apple", "type" : "cortland", "price" : Decimal128("1.29") }
  { "_id" : 2, "item" : "apple", "type" : "fuji", "price" : Decimal128("1.99") }
  { "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : Decimal128("1.99") }
  ```

- Using the ordering of the index `{ price: 1 }`, [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) limits the query to the documents that are below the index key bound of `price` equal to `Decimal128("1.99")` and [`min()`](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min) limits the query to the documents that are at or above the index key bound of `price` equal to `Decimal128("1.39")`:

  **Note:**

  The bound specified by [`max()`](https://www.mongodb.com/docs/manual/reference/method/cursor.max.md#mongodb-method-cursor.max) must be greater than the bound specified by [`min()`.](https://www.mongodb.com/docs/manual/reference/method/cursor.min.md#mongodb-method-cursor.min)

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

  The query returns the following documents:

  ```javascript
  { "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") }
  ```
