MongoDB Interview Questions
Start with what the application reads and writes together. Then check whether your document model, indexes and shard key still fit as the data grows.
Try answering aloud before opening each explanation.
Try the MongoDB practice demoFoundations
When should related data be embedded rather than referenced?
Embed bounded data read and updated with its parent. Reference independently accessed or unbounded children so one growing document does not become a bottleneck.
Does MongoDB's flexible schema prevent validation?
No. A collection validator can reject documents that do not meet required shapes or types. Plan how existing documents will be migrated before tightening validation.
What does a single-document atomic update guarantee?
Changes within the document apply atomically. Use a predicate that matches the expected current state if a concurrent modification should invalidate your update.
What happens if you index a field containing arrays?
MongoDB can build a multikey index with entries from the array. Check compound index restrictions and growth in index entries before indexing multiple array-like fields.
Why can a query using { field: null } return documents without that field?
The condition matches both explicit null and missing fields. Use an existence or type predicate when the distinction matters.
Applied
How do you choose a compound index for equality, sorting and a range?
Place common equality fields first. Decide whether supporting the sort or using a selective range is more valuable, and confirm the candidate against explain and representative data.
Why is it useful to move a selective $match early in an aggregation?
It reduces the rows downstream stages need to process and can allow indexed access near the start. Review the optimized plan; a filter depending on derived fields cannot run before those fields exist.
How can $unwind alter an aggregation's result count?
It emits one result per array element and normally omits null, missing or empty arrays. Configure preservation explicitly when you still need one output for those cases.
How would you paginate an activity feed with duplicate timestamps?
Order on timestamp and a unique tie-breaker, and apply a matching range predicate for the next page. Match the compound index to the filtering and sort order.
When would you use a partial index?
Use it when an important query consistently targets a defined subset, such as active records. Its predicate must be implied by queries using the index, or the index could omit relevant documents.
Production scenarios
What does a high documents-examined to results-returned ratio suggest?
The query may examine many candidates before filtering. Inspect its execution plan and predicate selectivity, then test an index or model change rather than tuning on timing alone.
What does majority write concern address after a primary failure?
It asks for acknowledgement from the required majority according to the deployment's rules. It reduces rollback exposure compared with primary-only acknowledgement but does not choose the read consistency of a later request.
Why can a shard key based on increasing timestamps create uneven write load?
New writes may concentrate in the current range even when historical data is balanced. Compare routing and query needs before using hashing or a compound key to distribute current writes.
Why avoid transactions for every multi-document read or write?
Transactions provide guarantees when an invariant spans documents, but can add contention and cost. First consider whether the invariant can live in one document, or whether an asynchronous projection is sufficient.
How would you change a document field without breaking rolling deployments?
Deploy readers that handle both forms, then update writers and backfill in bounded chunks. Enable the stricter validator only after old writers are gone and remaining legacy records are accounted for.
Further reading
Official documentation
Related question sets
- Node.js interview questions
- TypeScript interview questions
- REST API interview questions
- Microservices interview questions
- SQL interview questions
- Python interview questions
- JavaScript interview questions
- React interview questions
- Kubernetes interview questions
- System Design interview questions
- Java interview questions
- Docker interview questions
- PostgreSQL interview questions
- Redis interview questions
- NestJS interview questions
- RabbitMQ interview questions