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

# MongoDB Limits and Thresholds

This document provides a collection of hard and soft limitations of the MongoDB system. The limitations on this page apply to deployments hosted in Atlas and self-hosted MongoDB deployments, unless specified otherwise.

## MongoDB Atlas Limitations

The following limitations apply only to deployments hosted in MongoDB Atlas. If any of these limits present a problem for your organization, contact [Atlas support.](https://www.mongodb.com/docs/atlas/support/)

### MongoDB Atlas Cluster Limits

| Component | Limit |
| --- | --- |
| Shards in [multi-region clusters](https://www.mongodb.com/docs/atlas/cluster-config/multi-cloud-distribution/) | 12 |
| Shards in single-region clusters | 70 |
| [Cross-region network permissions](https://www.mongodb.com/docs/atlas/reference/faq/networking.md#std-label-faq-cross-region) for a multi-region cluster | 40. Additionally, a cluster in any [project](https://www.mongodb.com/docs/atlas/organizations-projects.md#std-label-projects) spans more than 40 regions, you can't create a multi-region cluster in this project. |
| [Electable nodes](https://www.mongodb.com/docs/manual/core/replica-set-elections.md#std-label-replica-set-elections) per replica set or shard | 7 |
| [Cluster tier](https://www.mongodb.com/docs/atlas/manage-clusters/#select-cluster-tier) for the [Config server](https://www.mongodb.com/docs/manual/core/sharded-cluster-config-servers.md#std-label-sharding-config-server) (default/minimum tier) | `M30` |

**Note:**

Increases above `M30` for the Config server tier require the [MongoDB Support Team](https://www.mongodb.com/docs/atlas/support/), rather than default self-service autoscaling.

### MongoDB Atlas Connection Limits and Cluster Tier

MongoDB Atlas limits concurrent incoming connections based on the cluster tier and [class](https://www.mongodb.com/docs/atlas/customize-storage.md#std-label-storage-class-ui). MongoDB Atlas connection limits apply per node. For sharded clusters, MongoDB Atlas connection limits apply per [mongos](https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router.md#std-label-sharding-read-operations) router. The number of [mongos](https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router.md#std-label-sharding-read-operations) routers is equal to the number of replica set nodes across all shards.

Your [read preference](https://www.mongodb.com/docs/manual/core/read-preference/) also contributes to the total number of connections that MongoDB Atlas can allocate for a given query.

MongoDB Atlas has the following connection limits for the specified cluster tiers:

| MongoDB Atlas Cluster Tier | Maximum Connections Per Node |
| --- | --- |
| `Free` | 500 |
| `Flex` | 500 |
| `M10` | 1500 |
| `M20` | 3000 |
| `M30` | 3000 |
| `M40` | 6000 |
| `M50` | 16000 |
| `M60` | 32000 |
| `M80` | 96000 |
| `M140` | 96000 |
| `M200` | 128000 |
| `M300` | 128000 |

| MongoDB Atlas Cluster Tier | Maximum Connections Per Node |
| --- | --- |
| `M40` | 4000 |
| `M50` | 16000 |
| `M60` | 32000 |
| `M80` | 64000 |
| `M200` | 128000 |
| `M300` | 128000 |
| `M400` | 128000 |
| `M700` | 128000 |

| MongoDB Atlas Cluster Tier | Maximum Connections Per Node |
| --- | --- |
| `Free` | 500 |
| `Flex` | 500 |
| `M10` | 1500 |
| `M20` | 3000 |
| `M30` | 3000 |
| `M40` | 6000 |
| `M50` | 16000 |
| `M60` | 32000 |
| `M80` | 64000 |
| `M140` | 96000 |
| `M200` | 128000 |
| `M300` | 128000 |

**Note:**

MongoDB Atlas reserves a small number of connections to each cluster for supporting MongoDB Atlas services.

### MongoDB Atlas Multi-Cloud Connection Limitation

If you're connecting to a multi-cloud MongoDB Atlas deployment through a [private connection](https://www.mongodb.com/docs/atlas/reference/faq/connection-changes.md#std-label-conn-string-options), you can access only the nodes in the same cloud provider that you're connecting from. This cloud provider might not have the [primary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary) node in its region. When this happens, you must specify the [secondary read preference](https://www.mongodb.com/docs/manual/core/read-preference.md#std-label-read-preference) mode in the connection string to access the deployment.

If you need access to all nodes for your multi-cloud MongoDB Atlas deployment from your current provider through a private connection, you must perform one of the following actions:

- Configure a VPN in the current provider to each of the remaining providers.

- Configure a [private endpoint](https://www.mongodb.com/docs/atlas/security-configure-private-endpoints.md#std-label-private-endpoint) to MongoDB Atlas for each of the remaining providers.

### MongoDB Atlas Collection and Index Limits

While there is no hard limit on the number of collections in a single MongoDB Atlas cluster, the performance of a cluster might degrade if it serves a large number of collections and indexes. Larger collections have a greater impact on performance.

The recommended maximum combined number of collections and indexes by MongoDB Atlas cluster tier are as follows:

| MongoDB Atlas Cluster Tier | Recommended Maximum |
| --- | --- |
| `M10` | 5,000 collections and indexes |
| `M20` / `M30` | 10,000 collections and indexes |
| `M40`/+ | 100,000 collections and indexes |

### MongoDB Atlas Organization and Project Limits

MongoDB Atlas deployments have the following organization and project limits:

| Component | Limit |
| --- | --- |
| [Database users](https://www.mongodb.com/docs/atlas/security-add-mongodb-users/) per MongoDB Atlas project | 100 |
| [Atlas users](https://www.mongodb.com/docs/atlas/access/manage-org-users/) per MongoDB Atlas project | 500 |
| Atlas users per MongoDB Atlas organization | 500 |
| API Keys per MongoDB Atlas organization | 500 |
| [Access list entries](https://www.mongodb.com/docs/atlas/security/ip-access-list/) per MongoDB Atlas Project | 200 |
| Users per MongoDB Atlas team | 250 |
| Teams per MongoDB Atlas project | 100 |
| Teams per MongoDB Atlas organization | 250 |
| Teams per MongoDB Atlas user | 100 |
| Organizations per MongoDB Atlas user | 250 |
| [Linked organizations](https://www.mongodb.com/docs/atlas/billing.md#std-label-cross-org-billing) per cross-organization configuration | 250 |
| Clusters per MongoDB Atlas project | 25 |
| Projects per MongoDB Atlas organization | 250 |
| [Custom MongoDB roles](https://www.mongodb.com/docs/atlas/security-add-mongodb-roles/) per MongoDB Atlas project | 100 |
| Assigned roles per database user | 100 |
| Hourly billing per MongoDB Atlas organization | $50 |
| [Federated database instances](https://www.mongodb.com/docs/atlas/data-federation.md#std-label-atlas-data-federation) per MongoDB Atlas project | 25 |
| Total Network Peering Connections per MongoDB Atlas project | 50. Additionally, MongoDB Atlas limits the number of nodes per [Network Peering connection](https://www.mongodb.com/docs/atlas/security-vpc-peering.md#std-label-vpc-peering) based on the CIDR block and the [region](https://www.mongodb.com/docs/atlas/cloud-providers-regions/) selected for the project. |
| Pending network peering connections per MongoDB Atlas project | 25 |
| [AWS Private Link](https://www.mongodb.com/docs/atlas/security-private-endpoint.md#std-label-atlas-pl-limitations) addressable targets per region | 50 |
| [Azure PrivateLink](https://www.mongodb.com/docs/atlas/security-private-endpoint.md#std-label-atlas-pl-limitations) addressable targets per region | 150 |
| Unique shard keys per MongoDB Atlas-managed Global Cluster project | 40. This applies only to Global Clusters with [Atlas-Managed Sharding](https://www.mongodb.com/docs/atlas/tutorial/create-global-cluster/). There are no limits on the number of unique shard keys per project for [Global Clusters with Self-Managed Sharding.](https://www.mongodb.com/docs/atlas/shard-global-collection/) |
| `Free` clusters per MongoDB Atlas project | 1 |
| Number of [alert configurations](https://www.mongodb.com/docs/atlas/configure-alerts.md#std-label-configure-alerts) for each MongoDB Atlas project. | 500 |

### MongoDB Atlas Service Account Limits

MongoDB Atlas service accounts have the following organization and project limits:

| Component | Limit |
| --- | --- |
| [Atlas service accounts](https://www.mongodb.com/docs/atlas/configure-api-access/) per MongoDB Atlas organization | 200 |
| [Access list entries](https://www.mongodb.com/docs/atlas/security/ip-access-list/) per MongoDB Atlas service account | 200 |
| [Secrets](https://www.mongodb.com/docs/atlas/configure-api-access/) per MongoDB Atlas service account | 2 |
| [Active tokens](https://www.mongodb.com/docs/atlas/configure-api-access/) per MongoDB Atlas service account | 100 |

### MongoDB Atlas Label Limits

MongoDB Atlas limits the length and enforces ReGex requirements for the following component labels:

| Component | Character Limit | RegEx Pattern |
| --- | --- | --- |
| Cluster Name | 64 | `^([a-zA-Z0-9]([a-zA-Z0-9-]){0,21}(?<!-)([\w]{0,42}))$` |
| Project Name | 64 | `^[\p{L}\p{N}\-_.(),:&@+']{1,64}$` |
| Organization Name | 64 | `^[\p{L}\p{N}\-_.(),:&@+']{1,64}$` |
| API Key Description | 250 | |

If you have [peering-only mode enabled](https://www.mongodb.com/docs/atlas/reference/faq/connection-changes.md#std-label-atlas-faq-azure-gcp-peering-only), the cluster name character limit is 23.

MongoDB Atlas uses the first 23 characters of a cluster's name. These characters must be unique within the cluster's project. Cluster names with fewer than 23 characters can't end with a hyphen (`-`). Cluster names with more than 23 characters can't have a hyphen as the 23rd character.

Organization and project names can include any Unicode letter or number plus the following punctuation: `-_.(),:&@+'`.

### Free Cluster, and Flex Cluster Limitations

Additional limitations apply to MongoDB Atlas free clusters, and Flex clusters. To learn more, see the following resources:

- [Atlas Free Cluster Limitations](https://www.mongodb.com/docs/atlas/reference/free-shared-limitations/)

- [Atlas Flex Limitations](https://www.mongodb.com/docs/atlas/reference/flex-limitations/)

### MongoDB Atlas Command Limitations

Some MongoDB commands are unsupported in MongoDB Atlas. Additionally, some commands are supported only in MongoDB Atlas free clusters. To learn more, see the following resources:

- [Unsupported Commands in Atlas](https://www.mongodb.com/docs/atlas/unsupported-commands/)

- [Commands Available Only in Free Clusters](https://www.mongodb.com/docs/atlas/free-tier-commands/)

## Collection and Database Size Limits

MongoDB does not impose a hard limit on collection or database sizes. The maximum size of a collection or database depends on the file system of the host server. For example:

- **ext4**: Maximum file size of 16 tebibytes (TiB).

- **XFS**: Maximum file size of 8 exbibytes (EiB).

To scale beyond file system or hardware limits, MongoDB offers strategies such as [sharding](https://www.mongodb.com/docs/manual/sharding.md#std-label-sharding-sharded-cluster), which allows collections to grow indefinitely. For collections nearing these limits or performance bottlenecks, MongoDB recommends sharding or migrating to a clustered setup such MongoDB Atlas, which supports auto-scaling.

### Sharding and Initial Size

When sharding a collection, MongoDB determines initial chunk ranges based on the shard key and splits your data into manageable chunks. These limits only affect the initial sharding operation:

- `chunkSize` (default: 128 MB).

- Average Shard Key Size (typical size is 16 bytes).

## BSON Documents

The maximum BSON document size is 16 mebibytes.

The maximum document size helps ensure that a single document cannot use an excessive amount of RAM or, during transmission, an excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API. For more information about GridFS, see [`mongofiles`](https://www.mongodb.com/docs/database-tools/mongofiles.md#mongodb-binary-bin.mongofiles) and the documentation for your [driver.](https://www.mongodb.com/docs/drivers/)

MongoDB supports no more than 100 levels of nesting for [BSON documents](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-document). Each object or array adds a level.

## Naming Restrictions

Do not rely on case to distinguish between databases. For example, you cannot use two databases with names like, `salesData` and `SalesData`.

After you create a database in MongoDB, you must use consistent capitalization when you refer to it. For example, if you create the `salesData` database, do not refer to it using alternate capitalization such as `salesdata` or `SalesData`.

For MongoDB deployments running on Windows, database names cannot contain any of the following characters:

```none
/\. "$*<>:|?
```

Also database names cannot contain the null character.

For MongoDB deployments running on Unix and Linux systems, database names cannot contain any of the following characters:

```none
/\. "$
```

Also database names cannot contain the null character.

Database names cannot be empty and must be less than 64 bytes.

Collection names should begin with an underscore or a letter character, and *cannot*:

- contain the `$`.

- be an empty string (e.g. `""`).

- contain the null character.

- begin with the `system.` prefix. (Reserved for internal use.)

- contain `.system.`.

If your collection name includes special characters, such as the underscore character, or begins with numbers, then to access the collection use the [`db.getCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.getcollection.md#mongodb-method-db.getCollection) method in [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) or a [similar method for your driver.](https://www.mongodb.com/docs/drivers/)

Namespace Length:

The namespace length limit for unsharded collections and views is 255 bytes, and 235 bytes for sharded collections. For a collection or a view, the namespace includes the database name, the dot (`.`) separator, and the collection/view name (e.g. `<database>.<collection>`).

- Field names **cannot** contain the `null` character.

- The server permits storage of field names that contain dots (`.`) and dollar signs (`$`).

- MongodB 5.0 adds improved support for the use of (`$`) and (`.`) in field names. There are some restrictions. See [Field Name Considerations](https://www.mongodb.com/docs/manual/core/dot-dollar-considerations.md#std-label-crud-concepts-dot-dollar-considerations) for more details.

- Each field name must be unique within the document. You must not store documents with duplicate fields because MongoDB [CRUD](https://www.mongodb.com/docs/manual/crud.md#std-label-crud) operations might behave unexpectedly if a document has duplicate fields.

The field name `_id` is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array or regex. If the `_id` contains subfields, the subfield names cannot begin with a (`$`) symbol.

## Naming Warnings

**Warning:**

Use caution, the issues discussed in this section could lead to data loss or corruption.

### MongoDB does not support duplicate field names

The MongoDB Query Language doesn't support documents with duplicate field names:

- Although some BSON builders may support creating a BSON document with duplicate field names, inserting these documents into MongoDB isn't supported even if the insert succeeds, or appears to succeed.

- For example, inserting a BSON document with duplicate field names through a MongoDB driver may result in the driver silently dropping the duplicate values prior to insertion, or may result in an invalid document being inserted that contains duplicate fields. Querying those documents leads to inconsistent results.

- Updating documents with duplicate field names isn't supported, even if the update succeeds or appears to succeed.

Starting in MongoDB 6.1, to see if a document has duplicate field names, use the [`validate`](https://www.mongodb.com/docs/manual/reference/command/validate.md#mongodb-dbcommand-dbcmd.validate) command with the `full` field set to `true`. In any MongoDB version, use the [`$objectToArray`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/objecttoarray.md#mongodb-expression-exp.-objectToArray) aggregation operator to see if a document has duplicate field names.

### Avoid Ambiguous Field Names

Do not use a field name that is the same as the [dot notation](https://www.mongodb.com/docs/manual/core/document.md#std-label-document-dot-notation-embedded-fields) for an embedded field. If you have a document with an embedded field `{ "a" : { "b": ... } }`, other documents in that collection should **not** have a top-level field `"a.b"`.

If you can reference an embedded field and a top-level field in the same way, indexing and sharding operations happen on the embedded field. You cannot index or shard on the top-level field `"a.b"` while the collection has an embedded field that you reference in the same way.

For example, if your collection contains documents with both an embedded field `{ "a" : { "b": ... } }` and a top-level field `"a.b"`, indexing and sharding operations happen on the embedded field. It is not possible to index or shard on the top-level field `"a.b"` when your collection also contains an embedded field `{ "a" : { "b": ... } }`.

### Import and Export Concerns With Dollar Signs (`$`) and Periods (`.`)

Starting in MongoDB 5.0, document field names can be dollar (`$`) prefixed and can contain periods (`.`). However, [`mongoimport`](https://www.mongodb.com/docs/database-tools/mongoimport.md#mongodb-binary-bin.mongoimport) and [`mongoexport`](https://www.mongodb.com/docs/database-tools/mongoexport.md#mongodb-binary-bin.mongoexport) may not work as expected in some situations with field names that make use of these characters.

[MongoDB Extended JSON v2](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json.md#std-label-extended-json-high-level-ref-v2) cannot differentiate between type wrappers and fields that happen to have the same name as type wrappers. Do not use Extended JSON formats in contexts where the corresponding BSON representations might include dollar (`$`) prefixed keys. The [DBRef](https://www.mongodb.com/docs/manual/reference/database-references.md#std-label-dbref-explanation) mechanism is an exception to this general rule.

There are also restrictions on using [`mongoimport`](https://www.mongodb.com/docs/database-tools/mongoimport.md#mongodb-binary-bin.mongoimport) and [`mongoexport`](https://www.mongodb.com/docs/database-tools/mongoexport.md#mongodb-binary-bin.mongoexport) with periods (`.`) in field names. Since CSV files use the period (`.`) to represent data hierarchies, a period (`.`) in a field name will be misinterpreted as a level of nesting.

### Possible Data Loss With Dollar Signs (`$`) and Periods (`.`)

There is a small chance of data loss when using dollar (`$`) prefixed field names or field names that contain periods (`.`) if these field names are used in conjunction with unacknowledged writes ([write concern](https://www.mongodb.com/docs/manual/reference/write-concern.md#std-label-write-concern) `w=0`) on servers that are older than MongoDB 5.0.

When running [`insert`](https://www.mongodb.com/docs/manual/reference/command/insert.md#mongodb-dbcommand-dbcmd.insert), [`update`](https://www.mongodb.com/docs/manual/reference/command/update.md#mongodb-dbcommand-dbcmd.update), and [`findAndModify`](https://www.mongodb.com/docs/manual/reference/command/findandmodify.md#mongodb-dbcommand-dbcmd.findAndModify) commands, drivers that are 5.0 compatible remove restrictions on using documents with field names that are dollar (`$`) prefixed or that contain periods (`.`). These field names generated a client-side error in earlier driver versions.

The restrictions are removed regardless of the server version the driver is connected to. If a 5.0 driver sends a document to an older server, the document will be rejected without sending an error.

## Namespaces

The namespace length limit for unsharded collections and views is 255 bytes, and 235 bytes for sharded collections. For a collection or a view, the namespace includes the database name, the dot (`.`) separator, and the collection/view name (e.g. `<database>.<collection>`).

**See also:**

[Naming Restrictions](https://www.mongodb.com/docs/manual/reference/limits.md#std-label-faq-restrictions-on-collection-names)

## Indexes

A single collection can have *no more* than 64 indexes.

There can be no more than 32 fields in a compound index.

You cannot combine the [`$text`](https://www.mongodb.com/docs/manual/reference/operator/query/text.md#mongodb-query-op.-text) query, which requires a special [text index](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-text/create-text-index.md#std-label-create-text-index), with a query operator that requires a different type of special index. For example you cannot combine [`$text`](https://www.mongodb.com/docs/manual/reference/operator/query/text.md#mongodb-query-op.-text) query with the [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) operator.

Fields with [2dsphere](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2dsphere.md#std-label-2dsphere-index) indexes must hold geometry data in the form of [coordinate pairs](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-legacy-coordinate-pairs) or [GeoJSON](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-GeoJSON) data. If you attempt to insert a document with non-geometry data in a `2dsphere` indexed field, or build a `2dsphere` index on a collection where the indexed field has non-geometry data, the operation will fail.

**See also:**

The unique indexes limit in [Sharding Operational Restrictions.](https://www.mongodb.com/docs/manual/reference/limits.md#std-label-limits-sharding-operations)

To generate keys for a 2dsphere index, [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) maps [GeoJSON shapes](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geospatial-indexes-store-geojson) to an internal representation. The resulting internal representation may be a large array of values.

When [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) generates index keys on a field that holds an array, [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) generates an index key for each array element. For compound indexes, [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) calculates the [cartesian product](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-cartesian-product) of the sets of keys that are generated for each field. If both sets are large, then calculating the cartesian product could cause the operation to exceed memory limits.

[`indexMaxNumGeneratedKeysPerDocument`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.indexMaxNumGeneratedKeysPerDocument) limits the maximum number of keys generated for a single document to prevent out of memory errors. The default is 100000 index keys per document. It is possible to raise the limit, but if an operation requires more keys than the [`indexMaxNumGeneratedKeysPerDocument`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.indexMaxNumGeneratedKeysPerDocument) parameter specifies, the operation will fail.

If the value of a field returned from a query that is [covered by an index](https://www.mongodb.com/docs/manual/core/query-optimization.md#std-label-covered-queries) is `NaN`, the type of that `NaN` value is *always* `double`.

[Multikey indexes](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-multikey.md#std-label-index-type-multikey) cannot cover queries over array fields.

Geospatial indexes can't [cover a query.](https://www.mongodb.com/docs/manual/core/query-optimization.md#std-label-covered-queries)

[`createIndexes`](https://www.mongodb.com/docs/manual/reference/command/createindexes.md#mongodb-dbcommand-dbcmd.createIndexes) supports building one or more indexes on a collection. `createIndexes` uses a combination of memory and temporary files on disk to build indexes. In self-hosted MongoDB deployments, the default value of the [`maxIndexBuildMemoryUsageMegabytes`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.maxIndexBuildMemoryUsageMegabytes) parameter is 200 megabytes per `createIndexes` command. `createIndexes` shares the memory limit equally among all the indexes it builds. For example, if you build 10 indexes with one `createIndexes` command, MongoDB allocates each index 20 megabytes for the index build process. When you reach the memory limit, MongoDB creates temporary files in the `_tmp` subdirectory within [`--dbpath`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--dbpath) to complete the build.

When using the default [`maxNumActiveUserIndexBuilds`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.maxNumActiveUserIndexBuilds) of `3`, the total memory usage for all concurrent index builds can reach up to three times the value of `maxIndexBuildMemoryUsageMegabytes`.

In self-hosted MongoDB deployments, you can change the memory limit for index builds by setting the `maxIndexBuildMemoryUsageMegabytes` parameter. Increasing this parameter is only necessary in rare cases. Such cases include running many simultaneous index builds with a single `createIndexes` command or indexing a data set larger than 500GB. You can also set `maxNumActiveUserIndexBuilds` to adjust how many index builds can run concurrently.

On MongoDB Atlas, the default values of `maxIndexBuildMemoryUsageMegabytes` and `maxNumActiveUserIndexBuilds` vary by cluster tier. You cannot modify `maxIndexBuildMemoryUsageMegabytes` on MongoDB Atlas.

The [`maxIndexBuildMemoryUsageMegabytes`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.maxIndexBuildMemoryUsageMegabytes) limit applies to all index builds initiated by user commands like [`createIndexes`](https://www.mongodb.com/docs/manual/reference/command/createindexes.md#mongodb-dbcommand-dbcmd.createIndexes) or administrative processes like [initial sync.](https://www.mongodb.com/docs/manual/core/replica-set-sync.md#std-label-replica-set-sync)

An [initial sync](https://www.mongodb.com/docs/manual/core/replica-set-sync.md#std-label-replica-set-sync) populates only one collection at a time and has no risk of exceeding the memory limit. However, it is possible for a user to start index builds on multiple collections in multiple databases simultaneously.

The following index types only support simple binary comparison and do not support [collation:](https://www.mongodb.com/docs/manual/reference/collation.md#std-label-collation)

- [Text](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-text.md#std-label-index-type-text) indexes

- [2d](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2d.md#std-label-2d-index) indexes

**Tip:**

To create a `text` or `2d` index on a collection that has a non-simple collation, you must explicitly specify `{collation: {locale: "simple"} }` when creating the index.

- You cannot [hide](https://www.mongodb.com/docs/manual/core/index-hidden.md#std-label-index-type-hidden) the `_id` index.

- You cannot use [`hint()`](https://www.mongodb.com/docs/manual/reference/method/cursor.hint.md#mongodb-method-cursor.hint) on a [hidden index.](https://www.mongodb.com/docs/manual/core/index-hidden.md)

## Sorts

- You can sort on a maximum of 32 keys.

- Providing a sort pattern with duplicate fields causes an error.

## Data

If you specify the maximum number of documents in a capped collection with [`create`](https://www.mongodb.com/docs/manual/reference/command/create.md#mongodb-dbcommand-dbcmd.create)'s `max` parameter, the value must be less than 2 31 documents.

If you do not specify a maximum number of documents when creating a capped collection, there is no limit on the number of documents.

## Replica Sets

Replica sets can have up to 50 members.

Replica sets can have up to 7 voting members. For replica sets with more than 7 total members, see [Non-Voting Members.](https://www.mongodb.com/docs/manual/core/replica-set-elections.md#std-label-replica-set-non-voting-members)

If you do not explicitly specify an oplog size (i.e. with [`oplogSizeMB`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-replication.oplogSizeMB) or [`--oplogSize`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--oplogSize)) MongoDB will create an oplog that is no larger than 50 gigabytes.&#x20;

The oplog can grow past its configured size limit to avoid deleting the [`majority commit point`.](https://www.mongodb.com/docs/manual/reference/command/replsetgetstatus.md#mongodb-data-replSetGetStatus.optimes.lastCommittedOpTime)

## Sharded Clusters

Sharded clusters have the restrictions and thresholds described here.

### Sharding Operational Restrictions

[`$where`](https://www.mongodb.com/docs/manual/reference/operator/query/where.md#mongodb-query-op.-where) does not permit references to the `db` object from the [`$where`](https://www.mongodb.com/docs/manual/reference/operator/query/where.md#mongodb-query-op.-where) function. This is uncommon in un-sharded collections.

When run on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-program-mongos), indexes can only [cover](https://www.mongodb.com/docs/manual/core/query-optimization.md#std-label-covered-queries) queries on [sharded](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard) collections if the index contains the shard key.

To use [`update`](https://www.mongodb.com/docs/manual/reference/command/update.md#mongodb-dbcommand-dbcmd.update) and [`remove()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.remove.md#mongodb-method-db.collection.remove) operations for a sharded collection that specify the `justOne` or `multi: false` option:

- If you only target one shard, you can use a partial shard key in the query specification or,

- You can provide the [shard key](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard-key) or the `_id` field in the query specification.

MongoDB does not support unique indexes across shards, except when the unique index contains the full shard key as a prefix of the index. In these situations MongoDB will enforce uniqueness across the full key, not a single field.

**See also:**

[Unique Constraints on Arbitrary Fields](https://www.mongodb.com/docs/manual/tutorial/unique-constraints-on-arbitrary-fields.md#std-label-shard-key-arbitrary-uniqueness) for an alternate approach.

By default, MongoDB cannot move a range if the number of documents in the range is greater than 2 times the result of dividing the configured [range size](https://www.mongodb.com/docs/manual/core/sharding-data-partitioning.md#std-label-sharding-range-size) by the average document size. If MongoDB can move a sub-range of a chunk and reduce the size to less than that, the balancer does so by migrating a range. [`db.collection.stats()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.stats.md#mongodb-method-db.collection.stats) includes the `avgObjSize` field, which represents the average document size in the collection.

For chunks that are [too large to migrate:](https://www.mongodb.com/docs/manual/core/sharding-balancer-administration.md#std-label-migration-chunk-size-limit)

- The balancer setting `attemptToBalanceJumboChunks` allows the balancer to migrate chunks too large to move as long as the chunks are not labeled [jumbo](https://www.mongodb.com/docs/manual/core/sharding-data-partitioning.md#std-label-jumbo-chunk). See [Balance Ranges that Exceed Size Limit](https://www.mongodb.com/docs/manual/tutorial/manage-sharded-cluster-balancer.md#std-label-balance-chunks-that-exceed-size-limit) for details.

  When issuing [`moveRange`](https://www.mongodb.com/docs/manual/reference/command/moverange.md#mongodb-dbcommand-dbcmd.moveRange) and [`moveChunk`](https://www.mongodb.com/docs/manual/reference/command/movechunk.md#mongodb-dbcommand-dbcmd.moveChunk) commands, it's possible to specify the [forceJumbo](https://www.mongodb.com/docs/manual/reference/command/moverange.md#std-label-moverange-forceJumbo) option to allow for the migration of ranges that are too large to move. The ranges may or may not be labeled [jumbo.](https://www.mongodb.com/docs/manual/core/sharding-data-partitioning.md#std-label-jumbo-chunk)

### Shard Key Limitations

A [shard key](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard-key) index can be an ascending index on the shard key, a compound index that starts with the shard key and specifies ascending order for the shard key, or a [hashed index.](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-hashed.md#std-label-index-type-hashed)

A [shard key](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard-key) index *cannot* be:

- A descending index on the shard key

- A [partial index](https://www.mongodb.com/docs/manual/core/index-partial.md#std-label-index-type-partial)

- Any of the following index types:

  - [Geospatial](https://www.mongodb.com/docs/manual/geospatial-queries.md#std-label-index-feature-geospatial)

  - [Multikey](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-multikey.md#std-label-index-type-multikey)

  - [Text](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-text.md#std-label-index-type-text)

  - [Wildcard](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-wildcard.md#std-label-wildcard-index-core)

Your options for changing a shard key depend on the version of MongoDB that you are running:

- Starting in MongoDB 5.0, you can [reshard a collection](https://www.mongodb.com/docs/manual/core/sharding-reshard-a-collection.md#std-label-sharding-resharding) by changing a document's shard key.

- You can [refine a shard key](https://www.mongodb.com/docs/manual/core/sharding-refine-a-shard-key.md#std-label-shard-key-refine) by adding a suffix field or fields to the existing shard key.

For clusters with high insert volumes, a shard key with monotonically increasing and decreasing keys can affect insert throughput. If your shard key is the `_id` field, be aware that the default values of the `_id` fields are [ObjectIds](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-ObjectId) which have generally increasing values.

When inserting documents with monotonically increasing shard keys, all inserts belong to the same [chunk](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-chunk) on a single [shard](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard). The system eventually divides the chunk range that receives all write operations and migrates its contents to distribute data more evenly. However, at any moment the cluster directs insert operations only to a single shard, which creates an insert throughput bottleneck.

If the operations on the cluster are predominately read operations and updates, this limitation may not affect the cluster.

To avoid this constraint, use a [hashed shard key](https://www.mongodb.com/docs/manual/core/hashed-sharding.md#std-label-sharding-hashed-sharding) or select a field that does not increase or decrease monotonically.

[Hashed shard keys](https://www.mongodb.com/docs/manual/core/hashed-sharding.md#std-label-sharding-hashed-sharding) and [hashed indexes](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-hashed.md#std-label-index-type-hashed) store hashes of keys with ascending values.

## Operations

If MongoDB cannot use an index or indexes to obtain the sort order, MongoDB must perform an in-memory sort operation on the data.

For more information on sorts and index use, see [Sort and Index Use.](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#std-label-sort-index-use)

MongoDB limits the number of [aggregation pipeline stages](https://www.mongodb.com/docs/manual/reference/mql/aggregation-stages.md#std-label-aggregation-pipeline-operator-reference) allowed in a single pipeline to 1000.

If an aggregation pipeline exceeds the stage limit before or after being parsed, you receive an error.

Starting in MongoDB 6.0, the [`allowDiskUseByDefault`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.allowDiskUseByDefault) parameter controls whether pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default.

- If [`allowDiskUseByDefault`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.allowDiskUseByDefault) is set to `true`, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default. You can disable writing temporary files to disk for specific `find` or `aggregate` commands using the `{ allowDiskUse: false }` option.

- If [`allowDiskUseByDefault`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.allowDiskUseByDefault) is set to `false`, pipeline stages that require more than 100 megabytes of memory to execute raise an error by default. You can enable writing temporary files to disk for specific `find` or `aggregate` using the `{ allowDiskUse: true }` option.

The [`$search`](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search) aggregation stage is not restricted to 100 megabytes of RAM because it runs in a separate process.

Examples of stages that can write temporary files to disk when [allowDiskUse](https://www.mongodb.com/docs/manual/reference/command/aggregate.md#std-label-aggregate-cmd-allowDiskUse) is `true` are:

- [`$bucket`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucket.md#mongodb-pipeline-pipe.-bucket)

- [`$bucketAuto`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucketauto.md#mongodb-pipeline-pipe.-bucketAuto)

- [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group)

- [`$setWindowFields`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setwindowfields.md#mongodb-pipeline-pipe.-setWindowFields)

- [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) when the sort operation is not supported by an index

- [`$sortByCount`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortbycount.md#mongodb-pipeline-pipe.-sortByCount)

**Note:**

Pipeline stages operate on streams of documents with each pipeline stage taking in documents, processing them, and then outputting the resulting documents.

Some stages can't output any documents until they have processed all incoming documents. These pipeline stages must keep their stage output in RAM until all incoming documents are processed. As a result, these pipeline stages may require more space than the 100 MB limit.

If the results of one of your [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) pipeline stages exceed the limit, consider [adding a $limit stage.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#std-label-sort-limit-sequence)

In addition to the per-stage limit, a single operation can use no more memory than the [per-operation memory limit.](https://www.mongodb.com/docs/manual/core/aggregation-pipeline-limits.md#std-label-operation-memory-limit)

The [profiler log messages](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiler) and [diagnostic log messages](https://www.mongodb.com/docs/manual/reference/log-messages.md#std-label-log-messages-ref) includes a `usedDisk` indicator if any aggregation stage wrote data to temporary files due to [memory restrictions.](https://www.mongodb.com/docs/manual/core/aggregation-pipeline-limits.md#std-label-agg-memory-restrictions)

Starting in MongoDB 9.0, MongoDB limits the total amount of memory that a single query operation can use. The limit applies to the combined memory of all memory-tracked stages of the operation.

Tracked memory includes:

- Blocking stages such as [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) and [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort)

- Expression evaluation

- Query execution stages that buffer records, such as geospatial proximity searches, counts that use an index, and updates

By default, the limit is 1 gigabyte or 20% of the memory available to the server process, whichever is greater. MongoDB sets the default when the server starts.

To change the per-operation limit, set the [`internalQueryMaxMemoryUsageBytesPerOperation`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.internalQueryMaxMemoryUsageBytesPerOperation) parameter at startup, at runtime, or with query settings.

For memory-intensive expressions that the per-operation limit does not cover, MongoDB applies a limit on the maximum memory that a subset of expressions can use. The default of this limit is 100 megabytes. To change this limit, set the [`internalQueryMaxSingleExpressionMemoryUsageBytes`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.internalQueryMaxSingleExpressionMemoryUsageBytes) parameter at startup or at runtime.

The per-operation limit is separate from the 100 megabyte per-stage memory limit and doesn't change how stages spill to disk. To learn about the per-stage limit, see [Memory Restrictions.](https://www.mongodb.com/docs/manual/core/aggregation-pipeline-limits.md#std-label-agg-memory-restrictions)

If an operation exceeds the per-operation limit, MongoDB ends the operation and returns one of the following errors:

- [`QueryExceededMemoryLimitNoDiskUseAllowed`](https://www.mongodb.com/docs/manual/reference/error-codes.md#mongodb-error-292) when a stage that can spill to disk exceeds the limit and `allowDiskUse` is `false`. Run the operation with `{ allowDiskUse: true }` so that the stage spills to disk instead of failing.

- [`ExceededMemoryLimit`](https://www.mongodb.com/docs/manual/reference/error-codes.md#mongodb-error-146) for other operations that exceed the limit.

Most operations use much less memory than the limit. If an operation returns a memory limit error, reduce the memory that the operation requires by performing one or more of the following actions:

- Use an index to satisfy the sort so that MongoDB doesn't sort documents in memory. To learn more, see [Sort and Index Use.](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#std-label-sort-index-use)

- Add a [`$limit`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit.md#mongodb-pipeline-pipe.-limit) stage after a [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) stage so that the sort keeps only the documents you need. To learn more, see [$sort and $limit Memory Optimization.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#std-label-sort-limit-sequence)

- Filter documents with [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) as early as possible in the pipeline so that later stages process fewer documents. To learn more, see [Aggregation Pipeline Optimization.](https://www.mongodb.com/docs/manual/core/aggregation-pipeline-optimization.md#std-label-agg-pipeline-optimization)

To check per-operation memory usage, use these [`serverStatus`](https://www.mongodb.com/docs/manual/reference/command/serverstatus.md#mongodb-dbcommand-dbcmd.serverStatus) metrics:

- [`metrics.query.peakMemoryUsageOperation`](https://www.mongodb.com/docs/manual/reference/command/serverstatus.md#mongodb-serverstatus-serverstatus.metrics.query.peakMemoryUsageOperation)

- [`metrics.query.operationsFailedDueToMemoryLimit`](https://www.mongodb.com/docs/manual/reference/command/serverstatus.md#mongodb-serverstatus-serverstatus.metrics.query.operationsFailedDueToMemoryLimit)

- [`metrics.query.configuredMaxMemoryUsageBytesPerOperation`](https://www.mongodb.com/docs/manual/reference/command/serverstatus.md#mongodb-serverstatus-serverstatus.metrics.query.configuredMaxMemoryUsageBytesPerOperation)

* The [`$out`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out.md#mongodb-pipeline-pipe.-out) stage cannot be used in conjunction with read concern [`"linearizable"`](https://www.mongodb.com/docs/manual/reference/read-concern-linearizable.md#mongodb-readconcern-readconcern.-linearizable-). If you specify [`"linearizable"`](https://www.mongodb.com/docs/manual/reference/read-concern-linearizable.md#mongodb-readconcern-readconcern.-linearizable-) read concern for [`db.collection.aggregate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate.md#mongodb-method-db.collection.aggregate), you cannot include the [`$out`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out.md#mongodb-pipeline-pipe.-out) stage in the pipeline.

* The [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) stage cannot be used in conjunction with read concern [`"linearizable"`](https://www.mongodb.com/docs/manual/reference/read-concern-linearizable.md#mongodb-readconcern-readconcern.-linearizable-). That is, if you specify [`"linearizable"`](https://www.mongodb.com/docs/manual/reference/read-concern-linearizable.md#mongodb-readconcern-readconcern.-linearizable-) read concern for [`db.collection.aggregate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate.md#mongodb-method-db.collection.aggregate), you cannot include the [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) stage in the pipeline.

The [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage cannot include an accumulator expression with an empty field name.

For example, the following operation is invalid:

```javascript
db.inventory.aggregate( [
   { $group: { _id: null, "": { $avg: "$a" } } }
] )
```

**See also:**

- [`$or`](https://www.mongodb.com/docs/manual/reference/operator/query/or.md#mongodb-query-op.-or)

- [2d Index Internals](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2d/internals.md#std-label-2d-index-internals)

Using a `2d` index for queries on spherical data can return incorrect results or an error. For example, `2d` indexes don't support spherical queries that wrap around the poles.

- Valid longitude values are between `-180` and `180`, both inclusive.

- Valid latitude values are between `-90` and `90`, both inclusive.

For [`$geoIntersects`](https://www.mongodb.com/docs/manual/reference/operator/query/geointersects.md#mongodb-query-op.-geoIntersects) or [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin), if you specify a single-ringed polygon that has an area greater than a single hemisphere, include the custom MongoDB coordinate reference system in the [`$geometry`](https://www.mongodb.com/docs/manual/reference/operator/query/geometry.md#mongodb-query-op.-geometry) expression. Otherwise, [`$geoIntersects`](https://www.mongodb.com/docs/manual/reference/operator/query/geointersects.md#mongodb-query-op.-geoIntersects) or [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) queries for the complementary geometry. For all other GeoJSON polygons with areas greater than a hemisphere, [`$geoIntersects`](https://www.mongodb.com/docs/manual/reference/operator/query/geointersects.md#mongodb-query-op.-geoIntersects) or [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) queries for the complementary geometry.

For [multi-document transactions:](https://www.mongodb.com/docs/manual/core/transactions.md#std-label-transactions)

- You can create collections and indexes in transactions. For details, see [Create Collections and Indexes in a Transaction](https://www.mongodb.com/docs/manual/core/transactions.md#std-label-transactions-create-collections-indexes)

- The collections used in a transaction can be in different databases.

  **Note:**

  You cannot create new collections in cross-shard write transactions. For example, if you write to an existing collection in one shard and implicitly create a collection in a different shard, MongoDB cannot perform both operations in the same transaction.

- You cannot write to [capped](https://www.mongodb.com/docs/manual/core/capped-collections.md#std-label-manual-capped-collection) collections.

- You cannot use read concern [`"snapshot"`](https://www.mongodb.com/docs/manual/reference/read-concern-snapshot.md#mongodb-readconcern-readconcern.-snapshot-) when reading from a [capped](https://www.mongodb.com/docs/manual/core/capped-collections.md#std-label-manual-capped-collection) collection. (Starting in MongoDB 5.0)

- You cannot read/write to collections in the `config`, `admin`, or `local` databases.

- You cannot write to `system.*` collections.

- You cannot return the supported operation's query plan using `explain` or similar commands.

* For cursors created outside of a transaction, you cannot call [`getMore`](https://www.mongodb.com/docs/manual/reference/command/getmore.md#mongodb-dbcommand-dbcmd.getMore) inside the transaction.

* For cursors created in a transaction, you cannot call [`getMore`](https://www.mongodb.com/docs/manual/reference/command/getmore.md#mongodb-dbcommand-dbcmd.getMore) outside the transaction.

- You cannot specify the [`killCursors`](https://www.mongodb.com/docs/manual/reference/command/killcursors.md#mongodb-dbcommand-dbcmd.killCursors) command as the first operation in a [transaction.](https://www.mongodb.com/docs/manual/core/transactions.md#std-label-transactions)

  Additionally, if you run the `killCursors` command within a transaction, the server immediately stops the specified cursors. It does **not** wait for the transaction to commit.

The following operations are not allowed in transactions:

- Creating new collections in cross-shard write transactions. For example, if you write to an existing collection in one shard and implicitly create a collection in a different shard, MongoDB cannot perform both operations in the same transaction.

- [Explicit creation of collections](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-ddl-explicit), e.g. [`db.createCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.createcollection.md#mongodb-method-db.createCollection) method, and indexes, e.g. [`db.collection.createIndexes()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.createindexes.md#mongodb-method-db.collection.createIndexes) and [`db.collection.createIndex()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.createindex.md#mongodb-method-db.collection.createIndex) methods, when using a read concern level other than [`"local"`.](https://www.mongodb.com/docs/manual/reference/read-concern-local.md#mongodb-readconcern-readconcern.-local-)

- The [`listCollections`](https://www.mongodb.com/docs/manual/reference/command/listcollections.md#mongodb-dbcommand-dbcmd.listCollections) and [`listIndexes`](https://www.mongodb.com/docs/manual/reference/command/listindexes.md#mongodb-dbcommand-dbcmd.listIndexes) commands and their helper methods.

- Other non-CRUD and non-informational operations, such as [`createUser`](https://www.mongodb.com/docs/manual/reference/command/createuser.md#mongodb-dbcommand-dbcmd.createUser), [`getParameter`](https://www.mongodb.com/docs/manual/reference/command/getparameter.md#mongodb-dbcommand-dbcmd.getParameter), [`count`](https://www.mongodb.com/docs/manual/reference/command/count.md#mongodb-dbcommand-dbcmd.count), etc. and their helpers.

- Parallel operations. To update multiple namespaces concurrently, consider using the [`bulkWrite`](https://www.mongodb.com/docs/manual/reference/command/bulkwrite.md#mongodb-dbcommand-dbcmd.bulkWrite) command instead.

Transactions have a lifetime limit as specified by [`transactionLifetimeLimitSeconds`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.transactionLifetimeLimitSeconds). The default is 60 seconds.

While MongoDB doesn't have a limit on the number of batch operations it can perform, it allows a maximum of 100,000 [writes](https://www.mongodb.com/docs/manual/reference/mql/crud-commands.md#std-label-query-and-write-commands) in a single batch operation. A single batch refers to a single request to the server. If the number of writes in a batch operation exceeds 100,000, the client driver divides the batch into smaller groups with counts less than or equal to 100,000.

A view definition `pipeline` cannot include the [`$out`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out.md#mongodb-pipeline-pipe.-out) or the [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) stage. This restriction also applies to embedded pipelines, such as pipelines used in [`$lookup`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup.md#mongodb-pipeline-pipe.-lookup) or [`$facet`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/facet.md#mongodb-pipeline-pipe.-facet) stages.

Views have the following operation restrictions:

- Views are read-only.

- You cannot rename [views.](https://www.mongodb.com/docs/manual/core/views.md#std-label-views-landing-page)

- [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) operations on views do not support the following [find command projection operators:](https://www.mongodb.com/docs/manual/reference/mql/projection.md#std-label-projection-operators-ref)
  - [`$`](https://www.mongodb.com/docs/manual/reference/operator/projection/positional.md#mongodb-projection-proj.-)

  - [`$elemMatch`](https://www.mongodb.com/docs/manual/reference/operator/projection/elemmatch.md#mongodb-projection-proj.-elemMatch)

  - [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/projection/slice.md#mongodb-projection-proj.-slice)

  - [`$meta`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta.md#mongodb-expression-exp.-meta)

- [Views](https://www.mongodb.com/docs/manual/core/views.md#std-label-views-landing-page) do not support `$text`.

- [Views](https://www.mongodb.com/docs/manual/core/views.md#std-label-views-landing-page) do not support map-reduce operations.

`$`-Prefixed Field Path Restriction

The [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) and [`findAndModify()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findandmodify.md#mongodb-method-db.collection.findAndModify) projection cannot project a field that starts with `$` with the exception of the [DBRef fields.](https://www.mongodb.com/docs/manual/reference/database-references.md#std-label-dbref-explanation)

For example, the following operation is invalid:

```javascript
db.inventory.find( {}, { "$instock.warehouse": 0, "$item": 0, "detail.$price": 1 } )
```

`$` Positional Operator Placement Restriction

The [`$`](https://www.mongodb.com/docs/manual/reference/operator/projection/positional.md#mongodb-projection-proj.-) projection operator can only appear at the end of the field path, for example `"field.$"` or `"fieldA.fieldB.$"`.

For example, the following operation is invalid:

```javascript
db.inventory.find( { }, { "instock.$.qty": 1 } )
```

To resolve, remove the component of the field path that follows the [`$`](https://www.mongodb.com/docs/manual/reference/operator/projection/positional.md#mongodb-projection-proj.-) projection operator.

Empty Field Name Projection Restriction

[`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) and [`findAndModify()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findandmodify.md#mongodb-method-db.collection.findAndModify) projection cannot include a projection of an empty field name.

For example, the following operation is invalid:

```javascript
db.inventory.find( { }, { "": 0 } )
```

In previous versions, MongoDB treats the inclusion/exclusion of the empty field as it would the projection of non-existing fields.

Path Collision: Embedded Documents and Its Fields

You cannot project an embedded document with any of the embedded document's fields.

For example, consider a collection `inventory` with documents that contain a `size` field:

```javascript
{ ..., size: { h: 10, w: 15.25, uom: "cm" }, ... }
```

The following operation fails with a `Path collision` error because it attempts to project both `size` document and the `size.uom` field:

```javascript
db.inventory.find( {}, { size: 1, "size.uom": 1 } )
```

In previous versions, lattermost projection between the embedded documents and its fields determines the projection:

- If the projection of the embedded document comes after any and all projections of its fields, MongoDB projects the embedded document. For example, the projection document `{ "size.uom": 1, size: 1 }` produces the same result as the projection document `{ size: 1 }`.

- If the projection of the embedded document comes before the projection any of its fields, MongoDB projects the specified field or fields. For example, the projection document `{ "size.uom": 1, size:
  1, "size.h": 1 }` produces the same result as the projection document `{ "size.uom": 1, "size.h": 1 }`.

Path Collision: `$slice` of an Array and Embedded Fields

[`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) and [`findAndModify()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findandmodify.md#mongodb-method-db.collection.findAndModify) projection cannot contain both a [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/projection/slice.md#mongodb-projection-proj.-slice) of an array and a field embedded in the array.

For example, consider a collection `inventory` that contains an array field `instock`:

```javascript
{ ..., instock: [ { warehouse: "A", qty: 35 }, { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ], ... }
```

The following operation fails with a `Path
        collision` error:

```javascript
db.inventory.find( {}, { "instock": { $slice: 1 }, "instock.warehouse": 0 } )
```

In previous versions, the projection applies both projections and returns the first element (`$slice: 1`) in the `instock` array but suppresses the `warehouse` field in the projected element. Starting in MongoDB 4.4, to achieve the same result, use the [`db.collection.aggregate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate.md#mongodb-method-db.collection.aggregate) method with two separate [`$project`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project) stages.

`$` Positional Operator and `$slice` Restriction

[`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) and [`findAndModify()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findandmodify.md#mongodb-method-db.collection.findAndModify) projection cannot include [`$slice`](https://www.mongodb.com/docs/manual/reference/operator/projection/slice.md#mongodb-projection-proj.-slice) projection expression as part of a [`$`](https://www.mongodb.com/docs/manual/reference/operator/projection/positional.md#mongodb-projection-proj.-) projection expression.

For example, the following operation is invalid:

```javascript
db.inventory.find( { "instock.qty": { $gt: 25 } }, { "instock.$": { $slice: 1 } } )
```

In previous versions, MongoDB returns the first element (`instock.$`) in the `instock` array that matches the query condition; i.e. the positional projection `"instock.$"` takes precedence and the `$slice:1` is a no-op. The `"instock.$": {
        $slice: 1 }` does not exclude any other document field.

## Sessions

To use [Client Sessions and Causal Consistency Guarantees](https://www.mongodb.com/docs/manual/core/read-isolation-consistency-recency.md#std-label-sessions) with `$external` authentication users (Kerberos, LDAP, or X.509 users), usernames cannot be greater than 10k bytes.

Sessions that receive no read or write operations for 30 minutes *or* that are not refreshed using [`refreshSessions`](https://www.mongodb.com/docs/manual/reference/command/refreshsessions.md#mongodb-dbcommand-dbcmd.refreshSessions) within this threshold are marked as expired and can be closed by the MongoDB server at any time. Closing a session kills any in-progress operations and open cursors associated with the session. This includes cursors configured with [`noCursorTimeout()`](https://www.mongodb.com/docs/manual/reference/method/cursor.nocursortimeout.md#mongodb-method-cursor.noCursorTimeout) or a [`maxTimeMS()`](https://www.mongodb.com/docs/manual/reference/method/cursor.maxtimems.md#mongodb-method-cursor.maxTimeMS) greater than 30 minutes.

Consider an application that issues a [`db.collection.find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find). The server returns a cursor along with a batch of documents defined by the [`cursor.batchSize()`](https://www.mongodb.com/docs/manual/reference/method/cursor.batchsize.md#mongodb-method-cursor.batchSize) of the [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find). The session refreshes each time the application requests a new batch of documents from the server. However, if the application takes longer than 30 minutes to process the current batch of documents, the session is marked as expired and closed. When the application requests the next batch of documents, the server returns an error as the cursor was killed when the session was closed.

For operations that return a cursor, if the cursor may be idle for longer than 30 minutes, issue the operation within an explicit session using [`Mongo.startSession()`](https://www.mongodb.com/docs/manual/reference/method/mongo.startsession.md#mongodb-method-Mongo.startSession) and periodically refresh the session using the [`refreshSessions`](https://www.mongodb.com/docs/manual/reference/command/refreshsessions.md#mongodb-dbcommand-dbcmd.refreshSessions) command. For example:

```bash
var session = db.getMongo().startSession()
var sessionId = session
sessionId  // show the sessionId

var cursor = session.getDatabase("examples").getCollection("data").find().noCursorTimeout()
var refreshTimestamp = new Date() // take note of time at operation start

while (cursor.hasNext()) {

  // Check if more than 5 minutes have passed since the last refresh
  if ( (new Date()-refreshTimestamp)/1000 > 300 ) {
    print("refreshing session")
    db.adminCommand({"refreshSessions" : [sessionId]})
    refreshTimestamp = new Date()
  }

  // process cursor normally

}
```

In the example operation, the [`db.collection.find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) method is associated with an explicit session. The cursor is configured with [`noCursorTimeout()`](https://www.mongodb.com/docs/manual/reference/method/cursor.nocursortimeout.md#mongodb-method-cursor.noCursorTimeout) to prevent the server from closing the cursor if idle. The `while` loop includes a block that uses [`refreshSessions`](https://www.mongodb.com/docs/manual/reference/command/refreshsessions.md#mongodb-dbcommand-dbcmd.refreshSessions) to refresh the session every 5 minutes. Since the session will never exceed the 30 minute idle timeout, the cursor can remain open indefinitely.

For MongoDB drivers, defer to the [driver documentation](https://www.mongodb.com/docs/drivers/) for instructions and syntax for creating sessions.
