Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Transactions vs Atomic Document Writes in MongoDB
📖 Scenario: You are managing a small online bookstore database using MongoDB. You want to understand how to safely update book stock counts either by updating a single document atomically or by using transactions when multiple documents need to be updated together.
🎯 Goal: Build a MongoDB script that first sets up a collection with book documents, then configures a threshold for low stock, applies atomic updates to single documents, and finally uses a transaction to update multiple documents safely.
📋 What You'll Learn
Create a collection named books with three book documents having fields _id, title, and stock with exact values.
Create a variable lowStockThreshold set to 5 to identify low stock books.
Write an atomic update to decrease the stock of a single book with _id 1 by 2 units.
Write a transaction that decreases the stock of books with _id 2 and 3 by 1 unit each.
💡 Why This Matters
🌍 Real World
Managing inventory in an online bookstore requires safe updates to stock counts to avoid overselling or inconsistent data.
💼 Career
Understanding atomic updates and transactions in MongoDB is essential for backend developers and database administrators to ensure data integrity in multi-document operations.
Progress0 / 4 steps
1
DATA SETUP: Create the books collection with initial documents
Create a collection called books and insert exactly these three documents: { _id: 1, title: 'Learn MongoDB', stock: 10 }, { _id: 2, title: 'Mastering NoSQL', stock: 8 }, and { _id: 3, title: 'Database Basics', stock: 6 }.
MongoDB
Hint
Use db.books.insertMany() with an array of the three exact book documents.
2
CONFIGURATION: Define the low stock threshold
Create a variable called lowStockThreshold and set it to 5 to mark books with stock less than or equal to this as low stock.
MongoDB
Hint
Use const lowStockThreshold = 5 to create the variable.
3
CORE LOGIC: Atomically update stock of a single book
Write a MongoDB update command to atomically decrease the stock of the book with _id 1 by 2 units using updateOne and the $inc operator.
MongoDB
Hint
Use db.books.updateOne({ _id: 1 }, { $inc: { stock: -2 } }) to atomically decrease stock.
4
COMPLETION: Use a transaction to update multiple books' stock
Write a MongoDB transaction that decreases the stock of books with _id 2 and 3 by 1 unit each. Use session.startTransaction(), two updateOne calls with $inc, and session.commitTransaction().
MongoDB
Hint
Use session.startTransaction(), update both documents with updateOne passing the session option, then session.commitTransaction().
Practice
(1/5)
1. Which statement best describes atomic document writes in MongoDB?
easy
A. They require manual rollback on failure.
B. They group multiple documents to update together.
C. They allow partial updates on multiple documents.
D. They update a single document completely or not at all.
Solution
Step 1: Understand atomic writes scope
Atomic writes in MongoDB apply only to single documents, ensuring full update or no update.
Step 2: Compare with multi-document operations
Multi-document updates require transactions, not atomic writes.
Final Answer:
They update a single document completely or not at all. -> Option D
Quick Check:
Atomic writes = single document update [OK]
Hint: Atomic writes affect one document fully or not at all [OK]
Common Mistakes:
Thinking atomic writes cover multiple documents
Confusing atomic writes with transactions
Assuming partial updates are atomic
2. Which of the following is the correct way to start a transaction in MongoDB using the shell?
easy
A. session.startTransaction()
B. db.startTransaction()
C. transaction.begin()
D. start.transaction()
Solution
Step 1: Recall MongoDB transaction syntax
Transactions start on a session object using startTransaction() method.
Step 2: Verify options
Only session.startTransaction() matches the correct syntax.
5. You need to update a user's profile document and also add a log entry in a separate collection. Which approach is best to ensure both updates succeed or fail together?
hard
A. Perform two separate atomic document writes without transactions.
B. Update the profile document atomically and ignore the log entry.
C. Use a transaction to update both collections atomically.
D. Update the log entry first, then the profile document without transactions.