Transactions¶
On this page
In MongoDB, an operation on a single document is atomic. Because you can use embedded documents and arrays to capture relationships between data in a single document structure instead of normalizing across multiple documents and collections, this single-document atomicity obviates the need for multi-document transactions for many practical use cases.
For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.
Transactions API¶
The following example highlights the key components of the transactions API:
- Python
- Java (Sync)
- Node.js
- PHP
- Motor
- Other
The example uses the new callback API for working with
transactions, which starts a transaction, executes the
specified operations, and commits (or aborts on error). The
new callback API also incorporates retry logic for
TransientTransactionError
or
UnknownTransactionCommitResult
commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with
transactions, which starts a transaction, executes the
specified operations, and commits (or aborts on error). The
new callback API also incorporates retry logic for
TransientTransactionError
or
UnknownTransactionCommitResult
commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with
transactions, which starts a transaction, executes the
specified operations, and commits (or aborts on error). The
new callback API also incorporates retry logic for
TransientTransactionError
or
UnknownTransactionCommitResult
commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with
transactions, which starts a transaction, executes the
specified operations, and commits (or aborts on error). The
new callback API also incorporates retry logic for
TransientTransactionError
or
UnknownTransactionCommitResult
commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with
transactions, which starts a transaction, executes the
specified operations, and commits (or aborts on error). The
new callback API also incorporates retry logic for
TransientTransactionError
or
UnknownTransactionCommitResult
commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with
transactions, which starts a transaction, executes the
specified operations, and commits (or aborts on error). The
new callback API also incorporates retry logic for
TransientTransactionError
or
UnknownTransactionCommitResult
commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with
transactions, which starts a transaction, executes the
specified operations, and commits (or aborts on error). The
new callback API also incorporates retry logic for
TransientTransactionError
or
UnknownTransactionCommitResult
commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
This example uses the core API. Because the core API does not incorporate retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors, the example includes explicit logic to retry the transaction for these errors:
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
This example uses the core API. Because the core API does not incorporate retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors, the example includes explicit logic to retry the transaction for these errors:
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
The example uses the new callback API for working with transactions, which starts a transaction, executes the specified operations, and commits (or aborts on error). The new callback API incorporates retry logic for "TransientTransactionError" or "UnknownTransactionCommitResult" commit errors.
Important
- Recommended. Use the MongoDB driver updated for the version of your MongoDB deployment. For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
- When using the drivers, each operation in the transaction must be associated with the session (i.e. pass in the session to each operation).
- Operations in a transaction use transaction-level read concern, transaction-level write concern, and transaction-level read preference.
- In MongoDB 4.2 and earlier, you cannot create collections in
transactions. Write operations that result in document inserts
(e.g.
insert
or update operations withupsert: true
) must be on existing collections if run inside transactions. - Starting in MongoDB 4.4, you can create collections in transactions implicitly or explicitly. You must use MongoDB drivers updated for 4.4, however. See Create Collections and Indexes In a Transaction for details.
See also
For an example in mongo
shell, see mongo Shell Example.
Transactions and Atomicity¶
Distributed Transactions and Multi-Document Transactions
Starting in MongoDB 4.2, the two terms are synonymous. Distributed transactions refer to multi-document transactions on sharded clusters and replica sets. Multi-document transactions (whether on sharded clusters or replica sets) are also known as distributed transactions starting in MongoDB 4.2.
For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions:
In version 4.0, MongoDB supports multi-document transactions on replica sets.
In version 4.2, MongoDB introduces distributed transactions, which adds support for multi-document transactions on sharded clusters and incorporates the existing support for multi-document transactions on replica sets.
To use transactions on MongoDB 4.2 deployments(replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2.
Multi-document transactions are atomic (i.e. provide an “all-or-nothing” proposition):
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"
can read the results of write 1 without seeing write 2.When a transaction aborts, all data changes made in the transaction are discarded without ever becoming visible. For example, if any operation in the transaction fails, the transaction aborts and all data changes made in the transaction are discarded without ever becoming visible.
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
See also
Transactions and Operations¶
Distributed transactions can be used across multiple operations, collections, databases, documents, and, starting in MongoDB 4.2, shards.
For transactions:
You can specify read/write (CRUD) operations on existing collections. For a list of CRUD operations, see CRUD Operations.
When using feature compatibility version (fcv)
"4.4"
or greater, you can create collections and indexes in transactions. For details, see Create Collections and Indexes In a TransactionThe 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 collections. (Starting in MongoDB 4.2)
You cannot read/write to collections in the
config
,admin
, orlocal
databases.You cannot write to
system.*
collections.You cannot return the supported operation’s query plan (i.e.
explain
).
- For cursors created outside of a transaction, you cannot call
getMore
inside the transaction. - For cursors created in a transaction, you cannot call
getMore
outside the transaction. - Starting in MongoDB 4.2, you cannot specify
killCursors
as the first operation in a transaction.
For a list of operations not supported in transactions, see Restricted Operations.
Tip
When creating or dropping a collection immediately before
starting a transaction, if the collection is accessed within the
transaction, issue the create or drop operation with write
concern "majority"
to ensure that the transaction
can acquire the required locks.
Create Collections and Indexes In a Transaction¶
Starting in MongoDB 4.4 with feature compatibility version (fcv) "4.4"
, you can create collections and indexes inside a
multi-document transaction unless the
transaction is a cross-shard write transaction. With "4.2"
or less,
operations that affect the database catalog, such as creating or
dropping a collection or an index, are disallowed in transactions.
When creating a collection inside a transaction:
- You can implicitly create a collection, such as with:
- an insert operation against a non-existing collection, or
- an update/findAndModify operation with
upsert: true
against a non-existing collection.
- You can explicitly create a collection using the
create
command or its helperdb.createCollection()
.
When creating an index inside a transaction [1], the index to create must be on either:
- a non-existing collection. The collection is created as part of the operation.
- a new empty collection created earlier in the same transaction.
[1] | You can also run db.collection.createIndex() and
db.collection.createIndexes() on existing indexes to check
for existence. These operations return successfully without creating
the index. |
Restrictions¶
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.
For explicit creation of a collection or an index inside a transaction, the transaction read concern level must be
"local"
. Explicit creation is through:Command Method create
db.createCollection()
createIndexes
The parameter
shouldMultiDocTxnCreateCollectionAndIndexes
must betrue
(the default). When setting the parameter for a sharded cluster, set the parameter on all shards.
See also
Count Operation¶
To perform a count operation within a transaction, use the
$count
aggregation stage or the $group
(with a
$sum
expression) aggregation stage.
MongoDB drivers compatible with the 4.0 features provide a
collection-level API countDocuments(filter, options)
as a helper
method that uses the $group
with a $sum
expression
to perform a count. The 4.0 drivers have deprecated the count()
API.
Starting in MongoDB 4.0.3, the mongo
shell provides the
db.collection.countDocuments()
helper method that uses the
$group
with a $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()
method/thedistinct
command as well as the aggregation pipeline with the$group
stage.For sharded collections, you cannot use the
db.collection.distinct()
method or thedistinct
command.To find the distinct values for a sharded collection, use the aggregation pipeline with the
$group
stage instead. For example:Instead of
db.coll.distinct("x")
, useInstead of
db.coll.distinct("x", { status: "A" })
, use:
The pipeline returns a cursor to a document:
Iterate the cursor to access the results document.
Informational Operations¶
Informational commands, such as isMaster
,
buildInfo
, connectionStatus
(and their
helper methods) are allowed in transactions; however, they cannot be
the first operation in the transaction.
Restricted Operations¶
Changed in version 4.4.
The following operations are not allowed in transactions:
- Operations that affect the database catalog, such as creating or
dropping a collection or an index when using feature
compatibility version (fcv)
"4.2"
or lower. With fcv"4.4"
or greater, you can create collections and indexes in transactions unless the transaction is a cross-shard write transaction. For details, see Create Collections and Indexes In a Transaction. - 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, e.g.
db.createCollection()
method, and indexes, e.g.db.collection.createIndexes()
anddb.collection.createIndex()
methods, when using a read concern level other than"local"
. - The
listCollections
andlistIndexes
commands and their helper methods. - Other non-CRUD and non-informational operations, such as
createUser
,getParameter
,count
, etc. and their helpers.
Transactions and Sessions¶
- Transactions are associated with a session; i.e. you start a transaction for a session.
- At any given time, you can have at most one open transaction for a session.
- When using the drivers, each operation in the transaction must be associated with the session. Refer to your driver specific documentation for details.
- If a session ends and it has an open transaction, the transaction aborts.
Read Concern/Write Concern/Read Preference¶
Transactions and Read Preference¶
Operations in a transaction use the transaction-level read preference.
Using the drivers, you can set the transaction-level read preference at the transaction start:
- If the transaction-level read preference is unset, the transaction uses the session-level read preference.
- If transaction-level and the session-level read preference are unset,
the transaction uses the client-level read preference. By default,
the client-level read preference is
primary
.
Multi-document transactions that contain
read operations must use read preference primary
. All
operations in a given transaction must route to the same member.
Transactions and Read Concern¶
Operations in a transaction use the transaction-level read concern. That is, any read concern set at the collection and database level is ignored inside the transaction.
You can set the transaction-level read concern at the transaction start.
- If the transaction-level read concern is unset, the transaction-level read concern defaults to the session-level read concern.
- If transaction-level and the session-level read concern are unset,
the transaction-level read concern defaults to the client-level read
concern. By default, client-level read concern is
"local"
for reads against the primary. See also:
Transactions support the following read concern levels:
"local"
¶
- Read concern
"local"
returns the most recent data available from the node but can be rolled back. - For transactions on sharded cluster,
"local"
read concern cannot guarantee that the data is from the same snapshot view across the shards. If snapshot isolation is required, use "snapshot" read concern. - Starting in MongoDB 4.4, with feature compatibility version
(fcv)
"4.4"
or greater, you can create collections and indexes inside a transaction. If explicitly creating a collection or an index, the transaction must use read concern"local"
. Implicit creation of a collection can use any of the read concerns available for transactions.
"majority"
¶
- Read concern
"majority"
returns data that has been acknowledged by a majority of the replica set members (i.e. data cannot be rolled back) if the transaction commits with write concern “majority”. - If the transaction does not use write concern “majority” for the commit, the
"majority"
read concern provides no guarantees that read operations read majority-committed data. - For transactions on sharded cluster,
"majority"
read concern cannot guarantee that the data is from the same snapshot view across the shards. If snapshot isolation is required, use "snapshot" read concern.
"snapshot"
¶
- Read concern
"snapshot"
returns data from a snapshot of majority committed data if the transaction commits with write concern “majority”. - If the transaction does not use write concern “majority” for the commit, the
"snapshot"
read concern provides no guarantee that read operations used a snapshot of majority-committed data. - For transactions on sharded clusters, the
"snapshot"
view of the data is synchronized across shards.
Transactions and Write Concern¶
Transactions use the transaction-level write concern to commit the write operations. Write operations inside transactions must be issued without explicit write concern specification and use the default write concern. At commit time, the writes are then commited using the transaction-level write concern.
Tip
Do not explicitly set the write concern for the individual write operations inside a transaction. Setting write concerns for the individual write operations inside a transaction results in an error.
You can set the transaction-level write concern at the transaction start:
- If the transaction-level write concern is unset, the transaction-level write concern defaults to the session-level write concern for the commit.
- If the transaction-level write concern and the session-level write
concern are unset, transaction-level write concern defaults to the
client-level write concern. By default, client-level write concern is
w: 1
. See also Default MongoDB Read Concerns/Write Concerns.
Transactions support all write concern w values, including:
w: 1
¶
Write concern
w: 1
returns acknowledgement after the commit has been applied to the primary.Important
When you commit with
w: 1
, your transaction can be rolled back if there is a failover.When you commit with
w: 1
write concern, transaction-level"majority"
read concern provides no guarantees that read operations in the transaction read majority-committed data.When you commit with
w: 1
write concern, transaction-level"snapshot"
read concern provides no guarantee that read operations in the transaction used a snapshot of majority-committed data.
w: "majority"
¶
- Write concern
w: "majority"
returns acknowledgement after the commit has been applied to a majority (M) of voting members; i.e. the commit has been applied to the primary and (M-1) voting secondaries. - When you commit with
w: "majority"
write concern, transaction-level"majority"
read concern guarantees that operations have read majority-committed data. For transactions on sharded clusters, this view of the majority-committed data is not synchronized across shards. - When you commit with
w: "majority"
write concern, transaction-level"snapshot"
read concern guarantees that operations have from a synchronized snapshot of majority-committed data.
Note
Regardless of the write concern specified for the
transaction, the commit operation for a
sharded cluster transaction includes some parts that use {w:
"majority", j: true}
write concern.
General Information¶
Production Considerations¶
For various production considerations with using transactions, see Production Considerations. In addition, or sharded clusters, see also Production Considerations (Sharded Clusters).
Arbiters¶
Transactions whose write operations span multiple shards will error and abort if any transaction operation reads from or writes to a shard that contains an arbiter.
See also Disabled Read Concern Majority for transaction restrictions on shards that have disabled read concern majority.
Disabled Read Concern Majority¶
A 3-member PSA (Primary-Secondary-Arbiter) replica set or a sharded
cluster with 3-member PSA shards may have disabled read concern
majority (--enableMajorityReadConcern false
or
replication.enableMajorityReadConcern: false
)
- On sharded clusters,
If a transaction involves a shard that has disabled read concern “majority”, you cannot use read concern
"snapshot"
for the transaction. You can only use read concern"local"
or"majority"
for the transaction. If you use read concern"snapshot"
, the transaction errors and aborts.Transactions whose write operations span multiple shards will error and abort if any of the transaction’s read or write operations involves a shard that has disabled read concern
"majority"
.
- On replica set,
You can specify read concern
"local"
or"majority"
or"snapshot"
even in the replica set has disabled read concern “majority”.However, if you are planning to transition to a sharded cluster with disabled read concern majority shards, you may wish to avoid using read concern
"snapshot"
.
Tip
To check if read concern “majority” is disabled, You can run
db.serverStatus()
on the mongod
instances
and check the storageEngine.supportsCommittedReads
field. If false
, read concern “majority” is disabled.
For more information, see 3-Member Primary-Secondary-Arbiter Architecture and Three Member Primary-Secondary-Arbiter Shards.
Shard Configuration Restriction¶
You cannot run transactions on a sharded cluster that has a shard
with writeConcernMajorityJournalDefault
set to false
(such as a shard with a voting member that uses the in-memory
storage engine).
Note
Regardless of the write concern specified for the
transaction, the commit operation for a
sharded cluster transaction includes some parts that use {w:
"majority", j: true}
write concern.
Diagnostics¶
MongoDB provides various transactions metrics:
Via | |
---|---|
db.serverStatus() methodserverStatus command |
Returns transactions metrics. |
$currentOp aggregation pipeline |
Returns:
|
db.currentOp() methodcurrentOp command |
Returns:
|
mongod and mongos log messages |
Includes information on slow transactions (i.e. transactions
that exceed the operationProfiling.slowOpThresholdMs
threshhold) under the TXN log component. |
Feature Compatibility Version (FCV)¶
To use transactions, the featureCompatibilityVersion for all members of the deployment must be at least:
Deployment | Minimum featureCompatibilityVersion |
---|---|
Replica Set | 4.0 |
Sharded Cluster | 4.2 |
To check the fCV for a member, connect to the member and run the following command:
For more information, see the
setFeatureCompatibilityVersion
reference page.
Storage Engines¶
Starting in MongoDB 4.2, multi-document transactions are supported on replica sets and sharded clusters where:
- the primary uses the WiredTiger storage engine, and
- the secondary members use either the WiredTiger storage engine or the in-memory storage engines.
In MongoDB 4.0, only replica sets using the WiredTiger storage engine supported transactions.
Note
You cannot run transactions on a sharded cluster that has a shard
with writeConcernMajorityJournalDefault
set to
false
, such as a shard with a voting member that uses the
in-memory storage engine.
Additional Transactions Topics¶
- Drivers API
- Production Considerations
- Production Considerations (Sharded Clusters)
- Transactions and Operations
- To learn more about when to use transactions and if they support your use case, see the Are Transactions Right For You? presentation from MongoDB.live 2020.