0
0
MongoDBquery~10 mins

Transaction isolation in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Transaction isolation in MongoDB
Start Transaction
Read Data Snapshot
Perform Writes
Check for Conflicts
Commit
End Transaction
A transaction starts by reading a consistent snapshot of data, performs writes, then checks for conflicts before committing or aborting.
Execution Sample
MongoDB
const session = client.startSession();
session.startTransaction();
const doc = collection.findOne({ _id: 1 }, { session });
collection.updateOne({ _id: 1 }, { $set: { value: 10 } }, { session });
await session.commitTransaction();
session.endSession();
This code starts a transaction, reads a document, updates it, and commits the transaction ensuring isolation.
Execution Table
StepActionData StateTransaction StateResult
1Start transactionData snapshot takenActiveTransaction started with snapshot isolation
2Read document _id=1Value=5 (snapshot)ActiveRead consistent snapshot value 5
3Update document _id=1 to value=10Value=10 (uncommitted)ActiveUpdate staged in transaction
4Commit transactionValue=10 (committed)CommittedChanges made visible to others
5End transactionData stableEndedTransaction ends successfully
💡 Transaction ends after commit, changes are now visible and isolated from other operations.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
doc.valueN/A5 (snapshot)5 (snapshot)10 (committed)10
transaction.stateNoneActiveActiveCommittedEnded
Key Moments - 2 Insights
Why does the read at step 2 show value 5 even though the update changes it to 10 later?
Because MongoDB transactions use snapshot isolation, the read sees the data as it was at the start of the transaction (value 5), not the uncommitted update.
What happens if a conflict is detected before commit?
The transaction aborts and all changes are discarded, ensuring no partial updates are visible (not shown in this trace but important to know).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the transaction state after step 3?
AEnded
BActive
CCommitted
DAborted
💡 Hint
Check the 'Transaction State' column at step 3 in the execution table.
At which step does the updated value become visible to other operations?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at when the transaction state changes to 'Committed' and data state shows committed value.
If the transaction aborted at step 4, what would be the final value of doc.value?
A5
B10
Cnull
Dundefined
💡 Hint
Refer to the concept of snapshot isolation and that aborted transactions discard changes.
Concept Snapshot
MongoDB transactions use snapshot isolation.
Reads see data as it was at transaction start.
Writes are staged and only visible after commit.
Conflicts cause abort to keep data consistent.
Commit makes changes visible atomically.
Full Transcript
In MongoDB, a transaction starts by taking a snapshot of the data. All reads during the transaction see this snapshot, ensuring consistent data even if other writes happen concurrently. When you update data inside the transaction, changes are staged but not visible outside until you commit. If conflicts occur, the transaction aborts to avoid partial updates. Committing the transaction makes all changes visible at once, maintaining isolation and consistency.