Practice & Assessment
Test your understanding of Production MongoDB: Transactions, Replication, and Security
Multiple Choice Questions
5In a MongoDB replica set, what happens when the primary node becomes unreachable?
Why should MongoClient be instantiated once as a singleton in a Node.js app?
What write concern setting guarantees a write is on disk on the majority of replica set nodes before acknowledging?
What is the least-privileged role you should assign to an application's MongoDB user for a production app that only reads and writes data?
When running mongodump for a production backup on a replica set, which node should you target and why?
Coding Challenges
1Bank Transfer with Transactions
Write a Node.js function transferFunds(fromId, toId, amount) that moves money between two accounts in a MongoDB 'accounts' collection using a multi-document transaction. Requirements: (1) Verify the source account has sufficient balance before debiting. If not, abort the transaction with a descriptive error. (2) Debit from source and credit to destination atomically. (3) Insert a transaction record into a 'transfers' collection with fromId, toId, amount, and timestamp โ inside the same transaction. (4) Use w:'majority' write concern. Write a test that calls transferFunds twice: once successfully and once with insufficient funds. Input: seed two accounts with balances. Output: updated balances and printed transfer records. Estimated time: 30 minutes.
Mini Project
Production-Ready E-commerce Backend
Build a production-grade Node.js + Express e-commerce API using Mongoose with all production concerns addressed. Features: User model with bcrypt passwords and JWT auth middleware. Product model with category, price, and stock. Order model that uses a multi-document transaction to debit stock and create the order atomically. MongoDB connection as a singleton with maxPoolSize: 10. Input validation on all endpoints returning 400 with field-level error messages from Mongoose ValidationError. Environment variables for MongoDB URI and JWT secret via dotenv. A GET /health endpoint that checks DB connectivity. A POST /orders endpoint that fails with 409 if any product is out of stock. All routes tested with curl examples in a README.
