Scrivio Interview library

MongoDB

27 questions in 8 subtopics.

Embedding vs referencing

DebuggingFoundations7 min read
A blog stores every comment inside the post document as an array. A popular post now fails to accept new comments with an error about the document size. What happened and how would you remodel it?
DesignIntermediate6 min read
In a MongoDB order system, each order shows the product name and price at the time of purchase, while the product catalogue changes daily. How do you model products in orders, and what is the extended reference pattern?
ScenarioAdvanced6 min read
A team ported a normalised relational schema to MongoDB with 25 collections, and every page needs five or six $lookup stages, so latency is worse than before. How would you lead the redesign?

MongoDB indexes

ConceptFoundations6 min read
A query is db.orders.find({status: 'OPEN', customerId: 42}).sort({createdAt: -1}). Which compound index would you create, and what is the equality, sort, range guideline?
CodingFoundations6 min read
Create MongoDB indexes for: sessions that should be deleted 30 minutes after lastSeen; unique emails only for active users; and a search by any element of a tags array. Show the commands.
DebuggingIntermediate7 min read
explain('executionStats') shows a query with nReturned: 20, totalKeysExamined: 450000, totalDocsExamined: 450000 and a SORT stage that used disk. What is wrong and how do you fix it?
LeadershipAdvanced7 min read
Your MongoDB clusters have 40 to 60 indexes per collection, many created by different teams over years, and write latency and memory use keep climbing. How would you clean this up safely?

Aggregation pipeline

CodingFoundations6 min read
Write an aggregation pipeline over orders {customerId, status, total, createdAt} that returns the top 5 customers by total spent on completed orders in 2026.
InternalsIntermediate7 min read
An aggregation pipeline fails with an error that a stage exceeded its memory limit. Explain the memory rules for pipeline stages and your options.
DesignAdvanced6 min read
Product teams build dashboards with heavy aggregation pipelines directly on the production MongoDB replica set. Design a better approach for analytics.

Multi-document transactions

ConceptFoundations6 min read
When does MongoDB need a multi-document transaction, and when is a single-document update with atomic operators enough?
DebuggingIntermediate7 min read
Your service uses MongoDB transactions with the Java driver and occasionally fails with a WriteConflict error labelled TransientTransactionError, and long transactions are aborted after about a minute. Explain both and how to handle them.
LeadershipPrincipal6 min read
MongoDB added multi-document ACID transactions in 4.0 after years of advice to avoid needing them. What does that history mean when your teams pick between MongoDB and a relational database today?

Replica sets and read concerns

ConceptFoundations6 min read
Explain write concern w:1 vs w:'majority' and read concern 'local' vs 'majority' in a MongoDB replica set. What can go wrong with w:1?
ScenarioIntermediate6 min read
Users update their profile and immediately reload the page, but sometimes see the old data. The service reads with readPreference secondaryPreferred. Explain and fix it without sending all reads to the primary.
DesignAdvanced6 min read
Design a MongoDB replica set deployment across three regions for a service that needs to survive a full region outage and keep writes available. Discuss members, priorities and trade-offs.

Sharding in MongoDB

ConceptFoundations6 min read
What is a shard key in MongoDB, and why is a monotonically increasing key like createdAt or an ObjectId a poor choice for a write-heavy collection?
DebuggingIntermediate6 min read
One shard in a cluster is at 90 percent CPU while others are idle. The shard key is {tenantId: 1} and one tenant generates 40 percent of traffic. Explain and fix it.
LeadershipPrincipal8 min read
Your company plans to shard its main MongoDB cluster because it is growing 80 percent a year. As the senior architect, how do you decide whether to shard, scale up, split by domain, or change database?

Spring Data MongoDB

CodingFoundations6 min read
Using MongoTemplate in Spring Data MongoDB, write code that atomically increments the views field of an article by 1 and adds a tag only if it is not present, returning the updated document.
DebuggingIntermediate6 min read
A Spring Boot service with @Indexed(unique = true) on an email field allows duplicate emails in production but not in a developer's local setup. Why?
DesignIntermediate6 min read
How would you implement optimistic locking for concurrent edits of a document in Spring Data MongoDB, and what happens on conflict?
ScenarioAdvanced7 min read
A large Spring Data MongoDB codebase uses @DBRef everywhere, and loading lists is slow with many extra queries. How would you improve it across the codebase?

Schema validation and evolution

CodingFoundations5 min read
Add schema validation to an existing MongoDB users collection so email must be a string and age, if present, an integer of at least 0, without breaking writes to existing invalid documents.
DesignIntermediate6 min read
Documents in a MongoDB collection have three historical shapes of the address field. Describe the schema versioning pattern and how you would migrate without downtime.
LeadershipAdvanced6 min read
Teams treat MongoDB as schemaless and every service writes its own variations into shared collections. How do you introduce schema governance without slowing teams down?
LeadershipPrincipal6 min read
Ten years ago your company chose MongoDB partly to avoid schema migrations. Today data quality problems and inconsistent shapes cost a lot in every analytics project. What is your strategic response?