MongoDB
27 questions in 8 subtopics.
Embedding vs referencing
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?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?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
A query isdb.orders.find({status: 'OPEN', customerId: 42}).sort({createdAt: -1}). Which compound index would you create, and what is the equality, sort, range guideline?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.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?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
Write an aggregation pipeline over orders{customerId, status, total, createdAt} that returns the top 5 customers by total spent on completed orders in 2026.An aggregation pipeline fails with an error that a stage exceeded its memory limit. Explain the memory rules for pipeline stages and your options.Product teams build dashboards with heavy aggregation pipelines directly on the production MongoDB replica set. Design a better approach for analytics.Multi-document transactions
When does MongoDB need a multi-document transaction, and when is a single-document update with atomic operators enough?Your service uses MongoDB transactions with the Java driver and occasionally fails with aWriteConflict error labelled TransientTransactionError, and long transactions are aborted after about a minute. Explain both and how to handle them.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
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?Users update their profile and immediately reload the page, but sometimes see the old data. The service reads withreadPreference secondaryPreferred. Explain and fix it without sending all reads to the primary.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
What is a shard key in MongoDB, and why is a monotonically increasing key likecreatedAt or an ObjectId a poor choice for a write-heavy collection?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.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
UsingMongoTemplate 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.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?How would you implement optimistic locking for concurrent edits of a document in Spring Data MongoDB, and what happens on conflict?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
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.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.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?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?No question matches these choices.