HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
70 Topics
Chapter•360 Minutes

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.
Key Takeaways
  • Atomic operators prevent concurrency race conditions.
  • Updates occur directly on database server nodes.
Knowledge Check

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.
Key Takeaways
  • $ updates the first matching array item.
  • arrayFilters updates specific array elements matching complex condition filters.
Knowledge Check

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 (IXSCAN vs COLLSCAN).
  • Key Metrics: nReturned (matching documents), totalKeysExamined (indexed keys checked), totalDocsExamined (documents scanned). Target ratio: totalDocsExamined / nReturned close to 1.
Key Takeaways
  • Analyzes query execution performance.
  • Ideal ratio: totalKeysExamined $approx$ nReturned, with totalDocsExamined minimized.
Knowledge Check

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:

  1. All fields in the query filter are part of the index.
  2. All fields returned in the projection are part of the index.
  3. The _id field is explicitly excluded ({ _id: 0 }) unless indexed.
Key Takeaways
  • Zero document fetches (totalDocsExamined = 0).
  • Extremely fast; reads strictly from RAM index B-Trees.
Knowledge Check

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.

Key Takeaways
  • Fixed-size circular buffer storage.
  • Ideal for circular event logging, activity streams, and notification queues.
Knowledge Check

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).

Setting up a Change Stream in Node.js
const changeStream = User.watch();
changeStream.on('change', (change) => {
  console.log('Real-time DB Event:', change.operationType, change.fullDocument);
});
💡 Subscribes to live database modifications using Mongoose's model watch wrapper.
Key Takeaways
  • Provides real-time event notifications via the replication oplog.
  • Requires a Replica Set or Sharded Cluster.
Knowledge Check

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.
Key Takeaways
  • Ensures automatic failover and redundancy.
  • Contains 1 Primary and 1+ Secondary nodes.
Knowledge Check

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.
Key Takeaways
  • w: majority guarantees protection against rollback during failovers.
  • j: true ensures write durability across sudden crash events.
Knowledge Check

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).
Key Takeaways
  • Read Preference = Node routing target.
  • Read Concern = Data consistency level.
Knowledge Check

Which setting controls which replica set member node receives query read requests?

Q40: What is Sharding in MongoDB and what are its core components?

Sharding is MongoDB's strategy for horizontal scaling, distributing datasets across multiple physical server clusters (shards):

  • Shard: Replica set storing a subset of partitioned data.
  • Mongos: Query router routing client operations to matching shards.
  • Config Servers: Store cluster metadata and chunk mapping state.
Key Takeaways
  • Enables horizontal scale-out for massive datasets.
  • Components: Shards, mongos routers, Config Server replica sets.
Knowledge Check

Which component acts as the interface router for applications querying a MongoDB Sharded Cluster?