70 Progressive Questions (Easy to Advanced + Scenarios)
70 comprehensive interview questions spanning database basics, Mongoose ODM abstractions, advanced aggregation pipelines, system architecture, performance tuning, and real-world scenario challenges.
Q31: How do atomic update operators ($inc, $set, $push, $pull) work?
Update operators modify documents atomically directly on the database server without requiring client-side fetch-and-save loops:
$set: Sets value of a specified field.$inc: Increments field value by a specified number.$push/$pull: Appends or removes elements from array fields.
- Atomic operators prevent concurrency race conditions.
- Updates occur directly on database server nodes.
Which operator safely increments a counter field atomically without risking concurrent write race conditions?
Q32: How do positional operators ($ and $[<identifier>]) update nested array elements?
$(Positional Operator): Updates the first array element matching the query condition.arrayFilters($[elem]): Updates all nested array elements that satisfy specific matching conditions defined in the query options.
$updates the first matching array item.arrayFiltersupdates specific array elements matching complex condition filters.
Which update feature allows modifying specific nested array items matching a condition across an entire array?
Q33: How do you analyze query performance using .explain('executionStats')?
Executing .explain('executionStats') returns execution details for a query:
winningPlan: Selected execution path strategy.executionStages: Stage breakdown (IXSCANvsCOLLSCAN).- Key Metrics:
nReturned(matching documents),totalKeysExamined(indexed keys checked),totalDocsExamined(documents scanned). Target ratio:totalDocsExamined / nReturnedclose to 1.
- Analyzes query execution performance.
- Ideal ratio:
totalKeysExamined$approx$nReturned, withtotalDocsExaminedminimized.
In an explain plan output, what does totalDocsExamined = 10000 and nReturned = 5 indicate?
Q34: What is a Covered Query in MongoDB?
A Covered Query is a query that can be satisfied entirely from an index without inspecting any underlying documents from disk or memory. Criteria:
- All fields in the query filter are part of the index.
- All fields returned in the projection are part of the index.
- The
_idfield is explicitly excluded ({ _id: 0 }) unless indexed.
- Zero document fetches (
totalDocsExamined = 0). - Extremely fast; reads strictly from RAM index B-Trees.
What is totalDocsExamined for a fully Covered Query in an explain plan execution result?
Q35: What is a Capped Collection, and what are its characteristics?
A Capped Collection is a fixed-size collection that maintains insertion order. Once allocated size limits are reached, it automatically overwrites the oldest documents in a circular buffer pattern (FIFO). Writes are fast because no document relocation occurs. Updates that increase document size are prohibited.
- Fixed-size circular buffer storage.
- Ideal for circular event logging, activity streams, and notification queues.
What happens when a Capped Collection reaches its configured maximum byte capacity?
Q36: What are Change Streams and how do they work in real-time applications?
Change Streams allow applications to listen for real-time data changes across a collection, database, or deployment without polling. They leverage the replication oplog (oplog.rs) to emit change notification events (insert, update, delete, replace).
const changeStream = User.watch();
changeStream.on('change', (change) => {
console.log('Real-time DB Event:', change.operationType, change.fullDocument);
});- Provides real-time event notifications via the replication oplog.
- Requires a Replica Set or Sharded Cluster.
Which internal MongoDB component powers real-time Change Stream event notifications?
Q37: What is a Replica Set in MongoDB? Explain Primary, Secondary, and Arbiter nodes.
A Replica Set is a group of mongod instances that maintain the same data set for high availability and redundancy:
- Primary Node: Receives all write operations.
- Secondary Nodes: Replicate the Primary's oplog and apply operations asynchronously. Can serve read queries.
- Arbiter Node: Holds no data; participates strictly in elections to break tie votes.
- Ensures automatic failover and redundancy.
- Contains 1 Primary and 1+ Secondary nodes.
What is the primary role of an Arbiter node in a MongoDB Replica Set?
Q38: What is Write Concern in MongoDB (w: 1, w: majority, j: true)?
Write Concern describes the level of acknowledgment requested from MongoDB for write operations:
w: 1: Write acknowledged by Primary node only.w: 'majority': Write acknowledged by a majority of replica set data-bearing nodes.j: true: Write committed to on-disk journal file before returning success.
w: majorityguarantees protection against rollback during failovers.j: trueensures write durability across sudden crash events.
Which write concern setting guarantees that a write will NOT be rolled back if the Primary node crashes immediately after writing?
Q39: Explain Read Concern vs Read Preference.
- Read Preference: Controls where reads are routed (
primary,primaryPreferred,secondary,secondaryPreferred,nearest). - Read Concern: Controls data isolation and consistency level (
local,majority,linearizable,snapshot).
- Read Preference = Node routing target.
- Read Concern = Data consistency level.
Which setting controls which replica set member node receives query read requests?