MongoDB Interview Questions: Model, Index and Trade-Offs
How to answer MongoDB interview questions by naming the data shape, the access pattern and the cost you accept, with worked embed, index, SQL and consistency examples.
MongoDB interview questions test whether you can justify a document model, an index choice and a consistency decision against a concrete workload. For each question, state the shape of the data, the read and write patterns, and the cost you accept, then name the option you rejected. That structure turns a feature recital into a design conversation you can defend.
Why MongoDB questions start with the data shape
The first move is often a workload, not a definition: here is a system, would documents fit? Answer by naming the entities, which of them are read together, and how each one grows.
Take an orders system. An order has line items, the order and its items are almost always fetched together to render a confirmation or an order page, and the number of items per order is bounded by how a customer shops. So the default is one document: the order with its line items embedded as an array. One read returns everything the page needs, and there is no join.
Now contrast a product catalogue. A product has reviews, reviews are read on their own page, they arrive forever, and the review count is not bounded. Embedding them would grow the product document without limit, so reviews go in their own collection and reference the product. The parent stays small and the write path stays cheap.
The mistake to avoid is answering "MongoDB is schemaless" and stopping. Say instead that the schema is enforced by the application: validation on the write path, a shared model layer, and a check that every document written has the fields the read path expects. That answer shows you know where the discipline lives.
Embed or reference: the question behind the question
Use a decision rule you can state in one sentence: embed when the child is read with the parent, bounded in size, and updated with it; reference when it is shared, unbounded, or updated on its own.
A user profile with a handful of saved addresses fits the first case. Addresses are read with the profile, a person has a few, and they change when the profile changes, so they sit in an array on the profile document. An activity feed fits the second. It grows without limit, it is read on its own screen, and new entries arrive constantly, so it lives in a separate collection keyed by user.
Mention the 16 MB document limit as a real constraint, stated as a limit rather than a target. You are not trying to approach it; you are checking that your bounded assumption holds well below it.
Expect the follow-up: what happens when the array grows past your assumption? The honest answer is a migration path. Move the child into its own collection, keep a reference on the parent, and update the read path to fetch both. Say how you would find the affected documents and how you would run the change without downtime, even in outline.
Index questions: read the query, then choose
Start from the query and its sort, not from the list of fields. A compound index serves equality first, then sort, then range, because that order lets the engine walk the index in the order the query needs.
Suppose the common query filters on status and sorts by createdAt:
Query: find({ status: "open" }).sort({ createdAt: -1 })
Index: { status: 1, createdAt: -1 } chosen
Index: { createdAt: -1, status: 1 } rejectedThe chosen order matches equality on status, then the sort on createdAt, so the engine can use the index for both the filter and the ordering. The reverse order puts the sort field first, so the status filter cannot use the index as a prefix and the engine has to do more work. Say that out loud; it shows you read the query rather than the schema.
A covered query is one where the index alone answers the read: the filter and the projection both use only indexed fields, so the engine never fetches the document. If the page needs only status and createdAt, project exactly those fields and the index can serve it.
The mistake is indexing every field. Each extra index adds write cost, because every insert and update must maintain it, and it takes space. Name the queries an index serves and the write cost it adds, and drop the ones that serve nothing.
MongoDB vs SQL: answer with the workload, not the label
The comparison question is really a request to reason about the workload. Four criteria do most of the work: access pattern, transaction scope, schema stability, and how much joining the read path needs.
| Criterion | Favours documents | Favours relational |
|---|---|---|
| Access pattern | One entity read whole per request | Many entities combined per request |
| Transaction scope | Writes touch one document | Writes span several tables |
| Schema stability | Fields evolve per record | Schema is stable and shared |
| Join need | Little or no joining | Multi-table joins and ad hoc aggregates |
Two workloads make the table concrete. A reporting workload needs multi-table joins and ad hoc aggregates over changing questions; a relational store is the honest answer there, and saying so shows judgment rather than loyalty to a product. A per-user document read whole on every request, such as a profile with its settings, fits a document store because it removes the join entirely.
The mistake is declaring one model universally better. State the workload assumption that makes your choice true, and the answer stands even if the interviewer changes the assumption.
Consistency and replication questions in one breath
Read concern and write concern are knobs with a cost: stronger acknowledgement trades latency for durability. Say which you would set and what you are paying for it.
For a payment write, acknowledge from a majority of replica set members. The write waits for more nodes to confirm, so latency rises, and in return the record survives the loss of a minority of nodes. For a view counter, acknowledge from one node. The write returns quickly and a lost increment is acceptable, because the number is approximate anyway.
On failover, say what a candidate should say: during the window when a new primary is elected, some reads may return stale data, and the length of that window depends on your configuration. Then name which reads in your system can tolerate staleness and why. Product listings can; an account balance usually cannot.
The mistake is claiming strong consistency everywhere. It is expensive and usually unnecessary, and an interviewer will ask you to price it.
Aggregation and schema-evolution questions
Walk an aggregation pipeline out loud, stage by stage, naming each stage's input and output shape. For monthly totals per customer: match to the period, group by customer and month, then sort. Put the match first so it can use an index on the date field; a match after a group has nothing to index.
Stage 1 match { createdAt: within month } uses index on createdAt
Stage 2 group by { customerId, month } sums amount
Stage 3 sort by total descending small result setFor schema evolution, adding a field to new documents while old ones lack it is normal. Handle it on the read side with a default, so the code does not branch on presence everywhere. The mistake is treating the pipeline as a black box; narrate it and the interviewer can follow your reasoning even when a detail is off.
The one rule to carry into the room
For every MongoDB answer, say the data shape, the access pattern, the cost you accept, and the option you rejected. If you do not know a specific behaviour, say what you would check in the documentation and how you would test it. Practise the answer out loud in 90 seconds, then let a follow-up change one assumption and re-answer. That rehearsal is what backend interview answers that name trade-offs reward, and it carries into the sharding design interview walkthrough when the question widens.
Frequently asked questions
Do I need to know MongoDB syntax, or just the concepts?
Know enough syntax to write a filter, a projection, an index and a short aggregation pipeline, because interviewers often ask you to sketch one. The concepts carry more weight: why this shape, why this index order, what it costs. If you can write a basic query and explain the reasoning, you are in good shape.
What if the interviewer asks about sharding in a MongoDB context?
Answer with the shard key choice first: it should match the most common query and spread writes evenly. Name the cost of a poor key, such as hotspots or scatter-gather reads, and say what you would measure before committing. A short, honest answer beats a feature tour, and the sharding design interview walkthrough covers the reasoning in more depth.
How do I answer when I have used MongoDB only in a small project?
Say what you built, the shape you chose and why, and be explicit about the scale you did not reach. Then reason aloud about what would change at larger scale, such as splitting an embedded array or adding a compound index. Interviewers accept limited experience when the reasoning is sound and the boundaries are stated.
Is it acceptable to say I would use SQL for this workload?
Yes, when you give the reason. Name the joins, the transaction scope or the stable schema that makes a relational store the better fit, and say what you would lose by choosing documents instead. A justified recommendation shows judgment; a reflexive preference for either model does not.
How do I handle a question about a MongoDB feature I have never used?
Say plainly that you have not used it, then describe how you would find out: read the documentation for its guarantees and limits, write a small test against a local instance, and measure the behaviour you care about. That answer shows a working method, which is what the question is really testing.
Practising MongoDB answers? See how AI Interview assistant works.