0
0
MongoDBquery~30 mins

Read concern and write concern in transactions in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Read Concern and Write Concern in MongoDB Transactions
📖 Scenario: You are managing a small online bookstore database using MongoDB. You want to ensure that when you update book stock and sales records together, the data stays consistent and reliable. To do this, you will use transactions with specific read and write concerns.
🎯 Goal: Build a MongoDB transaction that updates the stock and sales collections with readConcern set to majority and writeConcern set to { w: 'majority' }. This ensures your transaction reads the most committed data and writes are acknowledged by the majority of nodes.
📋 What You'll Learn
Create a session to start a transaction
Set the transaction's read concern to 'majority'
Set the transaction's write concern to { w: 'majority' }
Update the 'stock' collection inside the transaction
Update the 'sales' collection inside the transaction
Commit the transaction
💡 Why This Matters
🌍 Real World
Transactions with read and write concerns are used in real-world applications to keep data consistent and reliable, especially in financial or inventory systems.
💼 Career
Understanding how to use transactions with read and write concerns is important for database administrators and backend developers working with MongoDB to ensure data integrity.
Progress0 / 4 steps
1
Setup MongoDB collections and start a client session
Create a MongoDB client connection called client. Then get the database bookstore and collections stock and sales. Finally, start a client session called session.
MongoDB
Need a hint?

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.

2
Configure transaction options with read and write concerns
Create a variable called transactionOptions that sets readConcern to { level: 'majority' } and writeConcern to { w: 'majority' }.
MongoDB
Need a hint?

Define transactionOptions as an object with readConcern and writeConcern properties set to the specified values.

3
Start the transaction and update collections inside it
Use 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.
MongoDB
Need a hint?

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.

4
Commit the transaction and end the session
Commit the transaction using session.commitTransaction(). Then end the session with session.endSession().
MongoDB
Need a hint?

Call await session.commitTransaction() to save changes. Then call session.endSession() to finish.