In Chapter 5, we discussed replica sets and sharding in MongoDB. In this chapter, we are going to discuss multidocument transactions in MongoDB, a new feature introduced in MongoDB 4.0.
Multidocument Transactions in MongoDB
In MongoDB, a write operation on single document is atomic, even if the write operation modifies multiple embedded documents within a single document. When a single write operation (e.g., db.collection.updateMany()) modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not.
Starting with version 4.0, MongoDB provides multidocument transactions for replica sets. Multidocument transactions help us to achieve all-or-nothing execution to maintain data integrity.
The multidocument transactions are atomic.
When a transaction commits, all data changes made in the transaction are saved and visible outside the transaction. The data changes are not visible outside the transaction until the transaction is committed.
When a transaction aborts, all data changes made in the transaction are discarded without ever becoming visible.
Note
Multidocument transactions are available only for a replica set. If we try to use multidocument transactions on a nonreplica set, we would get the error “Transaction numbers are only allowed on a replica set member or mongos,” as shown in Figure 6-1.
Limitations of Transactions
Transactions do have some limitations, which are given here.
You can specify CRUD operations only on existing collections. The collections can be in different databases.
You cannot perform read/write operations on config, admin, and local databases.
You cannot write to system.* collections.
You cannot create or drop indexes inside a transaction.
You cannot perform non-CRUD operations inside a transaction.
You cannot return an operation’s query plan (i.e., explain).
Transactions and Sessions
In MongoDB, transactions are associated with sessions. MongoDB’s sessions provide a framework that supports consistency and writes that can be retried. MongoDB’s sessions are available only for replica sets and shared clusters. A session is required to start the transaction. You cannot run a transaction outside a session, and a session can run only one transaction at a time. A session is essentially a context.
There are three commands that important in working with transactions.
session.startTransaction(): To start a new transaction in the current session.
session.commitTransaction(): To save changes made by the operations in the transaction.
session.abortTransaction(): To abort the transaction without saving it.
Recipe 6-1. Working with Multidocument Transactions
In this recipe, we are going to discuss how to work with multidocument transactions.
Problem
You want to work with multidocument transactions.
Solution
Use session.startTransaction(), session.commitTransaction(), and session.abortTransaction().
How It Works
Let’s follow the steps in this section to work with multidocument transactions.
Step 1: Multidocument Transactions
To work with multidocument transactions, first we create an employee collection under the employee database as shown here.