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.

Q41: What is a Shard Key, and how do you choose a good shard key?

A Shard Key determines how documents are partitioned across cluster shards. Characteristics of a good shard key:

  • High Cardinality: Contains many unique values.
  • Even Frequency Distribution: Avoids data clustering.
  • Non-Monotonically Increasing: Monotonic keys (e.g., auto-increment IDs, timestamps) cause all writes to hit a single shard (jumbo chunk hotspotting).
Key Takeaways
  • Immutable field determining document placement.
  • Avoid monotonically increasing keys; consider Hashed Shard Keys.
Knowledge Check

Why is a monotonically increasing timestamp a poor choice for a range-based Shard Key?

Q42: Compare Range-Based Sharding vs Hashed Sharding.

  • Range-Based Sharding: Partitions data into contiguous ranges based on shard key values. Efficient for range queries ($gte, $lte), but risks write hotspots if keys increase monotonically.
  • Hashed Sharding: Computes an MD5 hash of the key to distribute documents uniformly across shards. Excellent for write distribution, but range queries must scatter-gather across all shards.
Key Takeaways
  • Range-Based = Fast range queries, hotspot risk.
  • Hashed = Uniform write distribution, scatter-gather range queries.
Knowledge Check

What is the primary benefit of Hashed Sharding over Range-Based Sharding?

Q43: What is the WiredTiger Storage Engine and what features does it provide?

WiredTiger is the default storage engine for MongoDB. Key features:

  • Document-Level Concurrency: Uses optimistic concurrency control for concurrent document writes.
  • Snappy / Zlib Compression: Compresses data and indexes on disk to save up to 70% storage.
  • Memory Checkpoints & Journaling: Ensures durability and fast recovery.
Key Takeaways
  • Default storage engine since MongoDB 3.2.
  • Provides document-level concurrency locking and transparent data compression.
Knowledge Check

What concurrency control level does the WiredTiger storage engine provide for write operations?

Q44: How does Journaling work in MongoDB?

Journaling writes changes to an on-disk write-ahead log file before updating actual database files. If MongoDB shuts down unexpectedly or crashes, the server replays the journal log during restart to restore the dataset to a consistent state without data corruption.

Key Takeaways
  • Write-ahead logging for crash recovery.
  • Journal commits occur every 50ms by default.
Knowledge Check

What primary protection does Journaling provide in MongoDB?

Q45: What are Mongoose Discriminators and how do they enable schema inheritance?

Discriminators are a schema inheritance mechanism. They allow storing multiple related models with distinct schemas within a single underlying MongoDB collection. A hidden discriminator key (__t) tracks which model variant instantiated each document.

Mongoose Discriminator Inheritance
const options = { discriminatorKey: 'kind' };
const Event = mongoose.model('Event', new mongoose.Schema({}, options));

// Inherited Models in the SAME 'events' collection
const ClickedLinkEvent = Event.discriminator('ClickedLink',
  new mongoose.Schema({ url: String })
);
💡 Creates specialized event models backed by a unified underlying events collection.
Key Takeaways
  • Enables schema inheritance in a single collection.
  • Uses a discriminator key (default __t) to identify document types.
Knowledge Check

Where are documents created by Mongoose Discriminator models stored?

Q46: What are Retryable Writes and Retryable Reads?

Retryable Writes and Reads allow MongoDB drivers to automatically retry transient network errors or replica set failovers once without throwing application errors. Enabled by default in modern drivers (retryWrites=true). Operations must be idempotent.

Key Takeaways
  • Handles transient network glitches automatically.
  • Requires replica set deployments and idempotent operations.
Knowledge Check

Are Retryable Writes enabled by default in modern MongoDB drivers?

Q47: How does Connection Pooling work in Mongoose and MongoDB drivers?

Opening database connections is computationally expensive. MongoDB drivers maintain a Connection Pool—a cached cache of open socket connections reused for database requests. Configured via maxPoolSize (default 100 in modern drivers) and minPoolSize options.

Key Takeaways
  • Reuses cached socket connections.
  • Set maxPoolSize based on application concurrency needs.
Knowledge Check

What is the default maxPoolSize in modern MongoDB drivers?

Q48: What is an 'upsert' operation in MongoDB?

An upsert (update or insert) is a conditional write option. If a document matching the query criteria exists, MongoDB updates it. If no matching document exists, MongoDB creates and inserts a new document combining query filter keys and update field values.

Key Takeaways
  • Pass { upsert: true } in update queries.
  • Atomic operation preventing duplicate check-then-insert race conditions.
Knowledge Check

What happens when executing updateOne({ email: 'a@b.com' }, { $set: { name: 'A' } }, { upsert: true }) if no document matches?

Q49: How do $slice and$elemMatch function in projection specifiers?

  • $slice: Limits array items returned in query results (e.g., { comments: { $slice: 5 } } returns first 5 comments).
  • $elemMatch (Projection): Limits array output to contain strictly the first array element matching specified criteria.
Key Takeaways
  • $slice limits array output lengths or range offsets.
  • $elemMatch projection returns specific matching array items.
Knowledge Check

Which projection operator limits an array field output to return the LAST 3 items of an array?

Q50: What is Model.bulkWrite() and why is it superior to multiple individual queries?

bulkWrite() executes a batch of write operations (insertOne, updateOne, updateMany, deleteOne, replaceOne) in a single network round-trip to the database server. It drastically reduces network latency overhead when performing mass updates.

Key Takeaways
  • Batches write operations into a single network payload.
  • Can be ordered (stops on first error) or unordered (continues remaining writes).
Knowledge Check

What is the primary performance benefit of bulkWrite() over looping individual update queries?