Interview Questions
MongoDB Interview Questions Q: When would you choose MongoDB over PostgreSQL? Choose MongoDB when: documents naturally belong together and don't need to be join…
MongoDB Interview Questions
Q: When would you choose MongoDB over PostgreSQL?
Choose MongoDB when: documents naturally belong together and don't need to be joined (e.g., a blog post with comments, a product with variants), schema needs to evolve rapidly, you store hierarchical or polymorphic data. Choose PostgreSQL when: data is highly relational, you need strong ACID guarantees across many entities, complex queries with JOINs are common, or you need advanced analytics.
Q: What is embedding vs referencing in MongoDB?
Embedding stores related data inside the same document (fast reads, single query, document limit 16MB). Referencing stores an _id and requires a second query or $lookup (like a JOIN). Embed when data is always accessed together and has a 1:few relationship. Reference when data is large, frequently updated independently, or has many-to-many relationships.
Q: What is a replica set?
A group of MongoDB servers that maintain the same dataset. One primary accepts writes; secondaries replicate asynchronously. If the primary fails, an election promotes a secondary. Provides high availability and read scaling (read from secondaries). Atlas manages replica sets automatically.
Q: What is sharding?
Horizontal scaling by splitting data across multiple shards (servers) based on a shard key. Each shard holds a subset of the data. A mongos router directs queries to the right shard. Choose the shard key carefully — high cardinality, even distribution, commonly used in queries. Poor shard key choice leads to hotspots.
Q: Does MongoDB support transactions?
Yes, since v4.0 — multi-document ACID transactions across collections and databases on replica sets, and across shards since v4.2. However, transactions are more expensive than in relational DBs. The MongoDB data model encourages designing documents to avoid needing cross-document transactions.
Q: What is the aggregation pipeline?
A framework for data processing and transformation. Documents flow through an ordered series of stages ($match, $group, $sort, $lookup, $project, $unwind, etc.), each transforming the data. It's more powerful than find() for analytics, reporting, and data reshaping. Stages are processed server-side and can use indexes ($match and $sort at the start).
Q: What is the ESR rule for compound indexes?
Equality → Sort → Range. Put fields used in equality comparisons first, then fields used for sorting, then fields used in range queries. This makes the index as effective as possible because equal-value fields narrow the dataset most, sort fields allow in-order traversal, and range fields are at the end.