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.
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.
- Range-Based = Fast range queries, hotspot risk.
- Hashed = Uniform write distribution, scatter-gather range queries.
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.
- Default storage engine since MongoDB 3.2.
- Provides document-level concurrency locking and transparent data compression.
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.
- Write-ahead logging for crash recovery.
- Journal commits occur every 50ms by default.
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.
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 })
);- Enables schema inheritance in a single collection.
- Uses a discriminator key (default
__t) to identify document types.
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.
- Handles transient network glitches automatically.
- Requires replica set deployments and idempotent operations.
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.
- Reuses cached socket connections.
- Set
maxPoolSizebased on application concurrency needs.
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.
- Pass
{ upsert: true }in update queries. - Atomic operation preventing duplicate check-then-insert race conditions.
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.
$slicelimits array output lengths or range offsets.$elemMatchprojection returns specific matching array items.
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.
- Batches write operations into a single network payload.
- Can be ordered (stops on first error) or unordered (continues remaining writes).
What is the primary performance benefit of bulkWrite() over looping individual update queries?