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

# cursor.allowDiskUse() (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/)

Use [`allowDiskUse()`](https://www.mongodb.com/docs/manual/reference/method/cursor.allowdiskuse.md#mongodb-method-cursor.allowDiskUse) to either allow or prohibit writing temporary files on disk when a pipeline stage exceeds the 100 megabyte limit. Starting in MongoDB 6.0, operations that require greater than 100 megabytes of memory automatically write data to temporary files by default.

[`allowDiskUse()`](https://www.mongodb.com/docs/manual/reference/method/cursor.allowdiskuse.md#mongodb-method-cursor.allowDiskUse) has the following form:

```javascript
db.collection.find(<match>).sort(<sort>).allowDiskUse()
```

See [Sort and Index Use](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#std-label-sort-index-use) for more information on in-memory sort operations.

## 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 `allowDiskUseByDefault`

Starting in MongoDB 6.0, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default.

**Note:**

Prior to MongoDB 6.0, `.allowDiskUse(false)` and `.allowDiskUse(true)` have the same effect. In MongoDB 6.0, both `mongosh` and the legacy `mongo` shell behave the following way:

If [`allowDiskUseByDefault`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.allowDiskUseByDefault) is `true` (this is the default):

- `.allowDiskUse()` has no additional effect

- `.allowDiskUse(true)` has no additional effect

- `.allowDiskUse(false)` prohibits the query from writing temporary files to disk

If [`allowDiskUseByDefault`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.allowDiskUseByDefault) is `false`:

- `.allowDiskUse()` enables writing temporary files to disk

- `.allowDiskUse(true)` enables writing temporary files to disk

- `.allowDiskUse(false)` has no additional effect

### Supports Large Non-Indexed Sorts Only

[`cursor.allowDiskUse()`](https://www.mongodb.com/docs/manual/reference/method/cursor.allowdiskuse.md#mongodb-method-cursor.allowDiskUse) has no effect on sort operations answered using an index *or* non-indexed ("in-memory") sort operations which require less than 100 megabytes of memory. For more complete documentation on in-memory sorts and sort index use, see [Sort and Index Use.](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#std-label-sort-index-use)

To check if MongoDB must perform an in-memory sort, append [`cursor.explain()`](https://www.mongodb.com/docs/manual/reference/method/cursor.explain.md#mongodb-method-cursor.explain) to the query and check the [explain results](https://www.mongodb.com/docs/manual/reference/explain-results.md#std-label-explain-results). If the query plan contains a `SORT` stage, then MongoDB must perform an in-memory sort operation subject to the 100 megabyte memory limit.

## Example

Consider a collection `sensors` with only the default index on `_id`. The collection contains documents similar to the following:

```json
{
  "sensor-name" : "TEMP-21425",
  "sensor-location" : "Unit 12",
  "reading" : {
    "timestamp" : Timestamp(1580247215, 1),
    "value" : 212,
    "unit" : "Fahrenheit"
  }
}
```

The following operation includes a [`cursor.sort()`](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#mongodb-method-cursor.sort) on the field `reading.timestamp`. The operation also passes `false` to [`cursor.allowDiskUse()`](https://www.mongodb.com/docs/manual/reference/method/cursor.allowdiskuse.md#mongodb-method-cursor.allowDiskUse) to prohibit the query from writing temporary files to disk.

```javascript
db.sensors.find({"sensor-location" : "Unit 12"}).
  sort({"reading.timestamp" : 1}).
  allowDiskUse(false)
```

Since `reading.timestamp` is not included in an index, MongoDB must perform an in-memory sort operation to return results in the requested sort order. By specifying `cursor.allowDiskUse(false)`, MongoDB cannot process the sort operation if it requires more than 100 megabytes of system memory. If the operation requires more than 100 megabytes of system memory, MongoDB would return an error.
