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

# Transactions and Operations

MongoDB provides the ability to use transactions across multiple operations, collections, databases, documents, and shards.

## Operations Supported in Multi-Document Transactions

### CRUD Operations

The following read/write operations are allowed in transactions:

| Method | Command | Note |
| --- | --- | --- |
| [`db.collection.aggregate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.aggregate.md#mongodb-method-db.collection.aggregate) | [`aggregate`](https://www.mongodb.com/docs/manual/reference/command/aggregate.md#mongodb-dbcommand-dbcmd.aggregate) | Excluding the following stages: [`$collStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats.md#mongodb-pipeline-pipe.-collStats); [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentOp.md#mongodb-pipeline-pipe.-currentOp); [`$indexStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexStats.md#mongodb-pipeline-pipe.-indexStats); [`$listLocalSessions`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/listLocalSessions.md#mongodb-pipeline-pipe.-listLocalSessions); [`$listSessions`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/listSessions.md#mongodb-pipeline-pipe.-listSessions); [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge); [`$out`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out.md#mongodb-pipeline-pipe.-out); [`$planCacheStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/planCacheStats.md#mongodb-pipeline-pipe.-planCacheStats); [`$unionWith`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith.md#mongodb-pipeline-pipe.-unionWith) |
| [`db.collection.countDocuments()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.countDocuments.md#mongodb-method-db.collection.countDocuments) |  | Excluding the following query operator expressions: [`$where`](https://www.mongodb.com/docs/manual/reference/operator/query/where.md#mongodb-query-op.-where); [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near); [`$nearSphere`](https://www.mongodb.com/docs/manual/reference/operator/query/nearSphere.md#mongodb-query-op.-nearSphere) The method uses the [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) aggregation stage for the query and [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) aggregation stage with a [`$sum`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum.md#mongodb-group-grp.-sum) expression to perform the count. |
| [`db.collection.distinct()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.distinct.md#mongodb-method-db.collection.distinct) | [`distinct`](https://www.mongodb.com/docs/manual/reference/command/distinct.md#mongodb-dbcommand-dbcmd.distinct) | Available on unsharded collections. For sharded collections, use the aggregation pipeline with the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage. See [Distinct Operation](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-distinct). |
| [`db.collection.find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) | [`find`](https://www.mongodb.com/docs/manual/reference/command/find.md#mongodb-dbcommand-dbcmd.find) | |
| [`db.collection.deleteMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.deleteMany.md#mongodb-method-db.collection.deleteMany)[`db.collection.deleteOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.deleteOne.md#mongodb-method-db.collection.deleteOne)[`db.collection.remove()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.remove.md#mongodb-method-db.collection.remove) | [`delete`](https://www.mongodb.com/docs/manual/reference/command/delete.md#mongodb-dbcommand-dbcmd.delete) | |
| [`db.collection.findOneAndDelete()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndDelete.md#mongodb-method-db.collection.findOneAndDelete)[`db.collection.findOneAndReplace()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndReplace.md#mongodb-method-db.collection.findOneAndReplace)[`db.collection.findOneAndUpdate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndUpdate.md#mongodb-method-db.collection.findOneAndUpdate) | [`findAndModify`](https://www.mongodb.com/docs/manual/reference/command/findAndModify.md#mongodb-dbcommand-dbcmd.findAndModify) | If the update or replace operation is run with `upsert: true` on a non-existing collection, the collection is implicitly created. For more details, see [Administration Operations.](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-ddl) |
| [`db.collection.insertMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany.md#mongodb-method-db.collection.insertMany)[`db.collection.insertOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.insertOne.md#mongodb-method-db.collection.insertOne) | [`insert`](https://www.mongodb.com/docs/manual/reference/command/insert.md#mongodb-dbcommand-dbcmd.insert) | If run on a non-existing collection, the collection is implicitly created. For more details, see [Administration Operations.](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-ddl) |
| [`db.collection.updateOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateOne.md#mongodb-method-db.collection.updateOne)[`db.collection.updateMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateMany.md#mongodb-method-db.collection.updateMany)[`db.collection.replaceOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.replaceOne.md#mongodb-method-db.collection.replaceOne) | [`update`](https://www.mongodb.com/docs/manual/reference/command/update.md#mongodb-dbcommand-dbcmd.update) | If run on a non-existing collection, the collection is implicitly created. For more details, see [Administration Operations.](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-ddl) |
| [`db.collection.bulkWrite()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite.md#mongodb-method-db.collection.bulkWrite)Various [Bulk Operations](https://www.mongodb.com/docs/manual/reference/method.md#std-label-bulk-operation-methods) |  | If run on a non-existing collection, the collection is implicitly created. For more details, see [Administration Operations.](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-ddl) |

**Note: Updates to Shard Key Values**

You can update a document's shard key value (unless the shard key field is the immutable `_id` field) by issuing single-document update / findAndModify operations either in a transaction or as a [retryable write](https://www.mongodb.com/docs/manual/core/retryable-writes.md#std-label-retryable-writes). For details, see [Change a Document's Shard Key Value.](https://www.mongodb.com/docs/manual/core/sharding-change-shard-key-value.md#std-label-update-shard-key)

### Count Operation

To perform a count operation within a transaction, use the [`$count`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/count.md#mongodb-pipeline-pipe.-count) aggregation stage or the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) (with a [`$sum`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum.md#mongodb-group-grp.-sum) expression) aggregation stage.

MongoDB drivers provide a collection-level API `countDocuments(filter, options)` as a helper method that uses the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) with a [`$sum`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum.md#mongodb-group-grp.-sum) expression to perform a count.

[`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) provides the [`db.collection.countDocuments()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.countDocuments.md#mongodb-method-db.collection.countDocuments) helper method that uses the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) with a [`$sum`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum.md#mongodb-group-grp.-sum) expression to perform a count.

### Distinct Operation

To perform a distinct operation within a transaction:

- For unsharded collections, you can use the [`db.collection.distinct()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.distinct.md#mongodb-method-db.collection.distinct) method/the [`distinct`](https://www.mongodb.com/docs/manual/reference/command/distinct.md#mongodb-dbcommand-dbcmd.distinct) command as well as the aggregation pipeline with the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage.

- For sharded collections, you cannot use the [`db.collection.distinct()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.distinct.md#mongodb-method-db.collection.distinct) method or the [`distinct`](https://www.mongodb.com/docs/manual/reference/command/distinct.md#mongodb-dbcommand-dbcmd.distinct) command.

  To find the distinct values for a sharded collection, use the aggregation pipeline with the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage instead. For example:

  - Instead of `db.coll.distinct("x")`, use

    ```javascript
    db.coll.aggregate([
       { $group: { _id: null, distinctValues: { $addToSet: "$x" } } },
       { $project: { _id: 0 } }
    ])
    ```

  - Instead of `db.coll.distinct("x", { status: "A" })`, use:

    ```javascript
    db.coll.aggregate([
       { $match: { status: "A" } },
       { $group: { _id: null, distinctValues: { $addToSet: "$x" } } },
       { $project: { _id: 0 } }
    ])
    ```

  The pipeline returns a cursor to a document:

  ```javascript
  { "distinctValues" : [ 2, 3, 1 ] }
  ```

  Iterate the cursor to access the results document.

### Administration Operations

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 can create collections and indexes inside a [distributed transaction](https://www.mongodb.com/docs/manual/core/transactions.md#std-label-transactions-create-collections-indexes) if the transaction is not a cross-shard write transaction.

#### Explicit Create Operations

| Command | Method | Notes |
| --- | --- | --- |
| [`create`](https://www.mongodb.com/docs/manual/reference/command/create.md#mongodb-dbcommand-dbcmd.create) | [`db.createCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.createCollection.md#mongodb-method-db.createCollection) | See also the [Implicit Create Operations.](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-ddl-implicit) |
| [`createIndexes`](https://www.mongodb.com/docs/manual/reference/command/createIndexes.md#mongodb-dbcommand-dbcmd.createIndexes) | [`db.collection.createIndex()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.createIndex.md#mongodb-method-db.collection.createIndex)[`db.collection.createIndexes()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.createIndexes.md#mongodb-method-db.collection.createIndexes) | The index to create must either be on a non-existing collection, in which case, the collection is created as part of the operation, or on a new empty collection created earlier in the same transaction. |

**Note:**

For explicit creation of a collection or an index inside a transaction, the transaction read concern level must be [`"local"`.](https://www.mongodb.com/docs/manual/reference/read-concern-local.md#mongodb-readconcern-readconcern.-local-)

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

#### Implicit Create Operations

You can also implicitly create a collection through the following write operations against a non-existing collection:

| Method Run against Non-Existing Collection | Command Run against Non-Existing Collection |
| --- | --- |
| [`db.collection.findAndModify()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findAndModify.md#mongodb-method-db.collection.findAndModify) with `upsert: true`[`db.collection.findOneAndReplace()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndReplace.md#mongodb-method-db.collection.findOneAndReplace) with `upsert: true`[`db.collection.findOneAndUpdate()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndUpdate.md#mongodb-method-db.collection.findOneAndUpdate) with `upsert: true` | [`findAndModify`](https://www.mongodb.com/docs/manual/reference/command/findAndModify.md#mongodb-dbcommand-dbcmd.findAndModify) with `upsert: true` |
| [`db.collection.insertMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany.md#mongodb-method-db.collection.insertMany)[`db.collection.insertOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.insertOne.md#mongodb-method-db.collection.insertOne) | [`insert`](https://www.mongodb.com/docs/manual/reference/command/insert.md#mongodb-dbcommand-dbcmd.insert) |
| [`db.collection.updateOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateOne.md#mongodb-method-db.collection.updateOne) with `upsert: true`[`db.collection.updateMany()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.updateMany.md#mongodb-method-db.collection.updateMany) with `upsert: true`[`db.collection.replaceOne()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.replaceOne.md#mongodb-method-db.collection.replaceOne) with `upsert: true` | [`update`](https://www.mongodb.com/docs/manual/reference/command/update.md#mongodb-dbcommand-dbcmd.update) with `upsert: true` |
| [`db.collection.bulkWrite()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite.md#mongodb-method-db.collection.bulkWrite)  with insert or `upsert:true` operationsVarious [Bulk Operations](https://www.mongodb.com/docs/manual/reference/method.md#std-label-bulk-operation-methods) with insert or `upsert:true` operations | |

For other CRUD operations allowed in transactions, see [CRUD Operations.](https://www.mongodb.com/docs/manual/core/transactions-operations.md#std-label-transactions-operations-crud)

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

### Informational Operations

Informational commands, such as [`hello`](https://www.mongodb.com/docs/manual/reference/command/hello.md#mongodb-dbcommand-dbcmd.hello), [`buildInfo`](https://www.mongodb.com/docs/manual/reference/command/buildInfo.md#mongodb-dbcommand-dbcmd.buildInfo), [`connectionStatus`](https://www.mongodb.com/docs/manual/reference/command/connectionStatus.md#mongodb-dbcommand-dbcmd.connectionStatus) (and their helper methods) are allowed in transactions; however, they cannot be the first operation in the transaction.

## Restricted Operations

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

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

- Using 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)

- Reads/writes to collections in the `config`, `admin`, or `local` databases.

- Writes to `system.*` collections.

- Using `explain` or similar commands to return the supported operation's query plan.

- Calling [`getMore`](https://www.mongodb.com/docs/manual/reference/command/getMore.md#mongodb-dbcommand-dbcmd.getMore) on cursors created outside of a transaction, or calling [`getMore`](https://www.mongodb.com/docs/manual/reference/command/getMore.md#mongodb-dbcommand-dbcmd.getMore) outside of a transaction on cursors created within a transaction.

- Specifying 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)

  **Note:**

  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.

**See also:**

[Pending DDL Operations and Transactions](https://www.mongodb.com/docs/manual/core/transactions-production-consideration.md#std-label-txn-prod-considerations-ddl)
