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

# commitTransaction (database command)

## Definition

Saves the changes made by the operations in the [multi-document transaction](https://www.mongodb.com/docs/manual/core/transactions.md) and ends the transaction.

**Tip:**

In [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#std-program-mongosh), this command can also be run through the [`Session.commitTransaction()`](https://www.mongodb.com/docs/manual/reference/method/Session.commitTransaction.md#mongodb-method-Session.commitTransaction) and
[`Session.withTransaction()`](https://www.mongodb.com/docs/manual/reference/method/Session.withTransaction.md#mongodb-method-Session.withTransaction)
helper methods.

Helper methods are convenient for [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#std-program-mongosh) users, but they may not return the same level of information as database commands.  In cases where the convenience is not needed or the additional return fields are required, use the database command.

To run the [`commitTransaction`](https://www.mongodb.com/docs/manual/reference/command/commitTransaction.md#mongodb-dbcommand-dbcmd.commitTransaction), the command must be run against the `admin` database and run within a [`Session()`.](https://www.mongodb.com/docs/manual/reference/method/Session.md#mongodb-method-Session)

## Compatibility

This command 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

## Syntax

The command has the following syntax:

```javascript
db.adminCommand(
   {
     commitTransaction: 1,
     txnNumber: <long>,
     writeConcern: <document>,
     autocommit: false,
     comment: <any>
   }
)
```

## Behavior

### Write Concern

When committing the transaction, the session uses the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern.md) specified at the transaction start. See [`Session.startTransaction()`.](https://www.mongodb.com/docs/manual/reference/method/Session.startTransaction.md#mongodb-method-Session.startTransaction)

If you commit using the [`"w: 1"`](https://www.mongodb.com/docs/manual/reference/write-concern.md#mongodb-writeconcern-writeconcern.-number-) write concern, your transaction can be [rolled back if there is a failover..](https://www.mongodb.com/docs/manual/core/replica-set-rollbacks.md)

### Atomicity

When a transaction commits, all data changes made in the transaction are saved and visible outside the transaction. That is, a transaction will not commit some of its changes while rolling back others.

Until a transaction commits, the data changes made in the transaction are not visible outside the transaction.

However, when a transaction writes to multiple shards, not all outside read operations need to wait for the result of the committed transaction to be visible across the shards. For example, if a transaction is committed and write 1 is visible on shard A but write 2 is not yet visible on shard B, an outside read at read concern [`"local"`](https://www.mongodb.com/docs/manual/reference/read-concern-local.md#mongodb-readconcern-readconcern.-local-) can read the results of write 1 without seeing write 2.
