Read concern and write concern in transactions in MongoDB - Mini Project: Build & Apply
client. Then get the database bookstore and collections stock and sales. Finally, start a client session called session.Use new MongoClient('mongodb://localhost:27017') to connect. Use client.db('bookstore') to get the database. Use db.collection('stock') and db.collection('sales') to get collections. Use client.startSession() to start a session.
transactionOptions that sets readConcern to { level: 'majority' } and writeConcern to { w: 'majority' }.Define transactionOptions as an object with readConcern and writeConcern properties set to the specified values.
session.startTransaction(transactionOptions) to start the transaction. Then update the stock collection to decrease quantity by 1 for the book with title 'MongoDB Basics'. Next, update the sales collection to increase count by 1 for the same book. Use session in both update operations.Call session.startTransaction(transactionOptions). Use updateOne on stock and sales with the filter { title: 'MongoDB Basics' } and the update operators { $inc: { quantity: -1 } } and { $inc: { count: 1 } } respectively. Pass { session } as options.
session.commitTransaction(). Then end the session with session.endSession().Call await session.commitTransaction() to save changes. Then call session.endSession() to finish.
Practice
readConcern control in a MongoDB transaction?Solution
Step 1: Understand the role of readConcern
Read concern defines the level of isolation and freshness of data read during a transaction.Step 2: Differentiate from writeConcern
Write concern controls confirmation of writes, not reads.Final Answer:
How fresh or confirmed the data you read is during the transaction -> Option AQuick Check:
readConcern = data freshness [OK]
- Confusing readConcern with writeConcern
- Thinking readConcern controls write confirmations
- Assuming readConcern affects transaction speed
writeConcern in a MongoDB transaction?Solution
Step 1: Recall writeConcern syntax
Write concern uses the keywto specify acknowledgement level, e.g., 'majority'.Step 2: Check options for correctness
{ writeConcern: { w: 'majority' } } correctly useswriteConcern: { w: 'majority' }. Others misuse keys or mix readConcern and writeConcern.Final Answer:
{ writeConcern: { w: 'majority' } } -> Option AQuick Check:
writeConcern uses w key [OK]
- Using 'r' instead of 'w' for writeConcern
- Mixing readConcern and writeConcern keys
- Passing writeConcern as a string instead of object
const session = client.startSession();
session.startTransaction({ readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' } });
// transaction operations
await session.commitTransaction();Solution
Step 1: Identify readConcern in transaction options
The transaction is started withreadConcern: { level: 'snapshot' }.Step 2: Understand readConcern levels
'snapshot' means the transaction reads a consistent snapshot of data at start time.Final Answer:
snapshot -> Option DQuick Check:
readConcern level = snapshot [OK]
- Confusing writeConcern level with readConcern level
- Assuming default readConcern instead of specified
- Mixing up 'majority' with readConcern levels
const session = client.startSession();
session.startTransaction({ readConcern: { level: 'majority' }, writeConcern: { w: 1 } });
// transaction operations
await session.commitTransaction();Solution
Step 1: Check for session lifecycle management
The code does not callsession.endSession()aftercommitTransaction(). This is necessary to properly release session resources and avoid leaks.Step 2: Verify other components
readConcern level 'majority' is valid; writeConcern{ w: 1 }is valid (acknowledgement from primary); syntax is correct.Final Answer:
Missing session.endSession() call -> Option BQuick Check:
endSession after commit/abort [OK]
- Thinking readConcern 'majority' is invalid for transactions
- Assuming writeConcern w:1 is invalid
- Believing session.endSession() is optional
Solution
Step 1: Understand readConcern 'majority'
This level reads data confirmed by a majority of servers, ensuring recent confirmed data.Step 2: Understand writeConcern { w: 'majority' }
This ensures writes are acknowledged by a majority of servers before commit.Step 3: Evaluate other options
readConcern: 'local', writeConcern: { w: 1 } uses weaker writeConcern; C uses no write acknowledgement; D uses linearizable readConcern which is stricter than needed.Final Answer:
readConcern: 'majority', writeConcern: { w: 'majority' } -> Option CQuick Check:
Majority read and write concern = readConcern: 'majority', writeConcern: { w: 'majority' } [OK]
- Choosing writeConcern w:1 for majority confirmation
- Confusing 'snapshot' with 'majority' readConcern
- Ignoring writeConcern w:0 means no acknowledgement
Ready to prove it?
Pass the test and this pattern moves unknown
