0
0
MongoDBquery~10 mins

Multi-document transactions in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multi-document transactions
Start Transaction
Execute Operation 1
Execute Operation 2
Commit Transaction?
NoAbort Transaction
Yes
End Transaction
This flow shows how multiple operations on different documents are grouped in a transaction, which either commits all changes or aborts all to keep data consistent.
Execution Sample
MongoDB
session.startTransaction();
db.collection1.insertOne({a:1});
db.collection2.updateOne({b:2}, {$set: {c:3}});
session.commitTransaction();
This code starts a transaction, inserts a document in one collection, updates a document in another, then commits all changes together.
Execution Table
StepActionOperationState BeforeState AfterNotes
1Start TransactionBegin transaction sessionNo active transactionTransaction activeTransaction session started
2Insert DocumentInsert {a:1} into collection1collection1 has no {a:1}collection1 has {a:1} (pending)Insert staged, not visible outside transaction
3Update DocumentUpdate {b:2} in collection2 to set c=3collection2 has {b:2, c:old}collection2 has {b:2, c:3} (pending)Update staged, not visible outside transaction
4Commit TransactionCommit all changesTransaction active with staged changesChanges visible in both collectionsAll changes applied atomically
5End TransactionClose transaction sessionTransaction activeNo active transactionTransaction ended successfully
💡 Transaction commits successfully, making all changes visible together; if commit failed, all changes would be discarded.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Transaction StateNo transactionActiveActiveActiveCommittedNo transaction
collection1 documents{}{}{a:1} (pending){a:1} (pending){a:1} (committed){a:1}
collection2 documents{b:2, c:old}{b:2, c:old}{b:2, c:old}{b:2, c:3} (pending){b:2, c:3} (committed){b:2, c:3}
Key Moments - 2 Insights
Why don't the inserted and updated documents appear immediately outside the transaction?
Because the changes are staged inside the transaction and only become visible after commit, as shown in steps 2 and 3 where state after shows 'pending' changes.
What happens if the commit fails or is aborted?
All staged changes are discarded and the database returns to the state before the transaction started, preventing partial updates. This is implied by the exit note and the commit step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the transaction state after step 3?
ANo transaction
BActive with staged changes
CCommitted
DAborted
💡 Hint
Check the 'Transaction State' in variable_tracker after Step 3.
At which step do the changes become visible outside the transaction?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'State After' column in execution_table for when changes are 'committed'.
If the transaction is aborted after step 3, what happens to the inserted document in collection1?
AIt becomes visible but uncommitted
BIt is deleted
CIt is never inserted
DIt remains inserted
💡 Hint
Recall that uncommitted changes are not visible and are discarded on abort, as explained in key moments.
Concept Snapshot
Multi-document transactions group multiple operations into one atomic unit.
Start with startTransaction(), perform operations, then commitTransaction() to apply all changes.
If commit fails or abortTransaction() is called, all changes are discarded.
This ensures data consistency across multiple documents and collections.
Full Transcript
Multi-document transactions in MongoDB allow you to group several operations on different documents or collections into a single atomic unit. The process starts by calling startTransaction(), which opens a transaction session. Then, you perform your operations like inserts or updates. These changes are staged and not visible outside the transaction until you call commitTransaction(), which applies all changes together. If the commit fails or you call abortTransaction(), all staged changes are discarded, ensuring no partial updates occur. This mechanism helps keep your data consistent when multiple documents must be changed together.