🍃 MongoDB — interview questions
11 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.
11 of 11
01A query for orders containing a line with SKU "A-1" and quantity over 5 uses `{ "lines.sku": "A-1", "lines.qty": { $gt: 5 } }` and returns an order where the SKU-A-1 line has quantity 1 and a completely different line has quantity 20. What went wrong, and what's the fix?Querieseasy0-2y02A `Customer` document embeds an `orders` array because "a customer has orders," and it reads beautifully for a month. What breaks as the data grows, and how would you redesign it?Document modellingeasy0-2y03A `Product` collection embeds a `List<String> tags` via Spring Data MongoDB's `@Document` mapping, and a service does `product.getTags().add("sale")` then relies on a later `repository.save(product)` to persist it. It works in a quick manual test but a code reviewer flags it as fragile. What's the actual risk, and how would you make the write explicit and safe?Spring Data MongoDBmedium3-5y04An index exists as `{ status: 1, total: 1, placedAt: -1 }`, built for a query filtering on `status` and `total` and sorting by `placedAt`. The query is still slow and `explain()` shows a `SORT` stage. What's wrong with the index's field order, and how do you fix it?Indexesmedium3-5y05A payments service writes with the driver's default write concern, and after a primary failover, a write the client had already gotten a success response for is gone. What actually happened, and what write concern change closes the gap?Replication and consistencyhard5-8y06A team assumes the oplog is a literal record of the commands the application sent, and writes a tool that replays oplog entries against a second cluster for disaster recovery by re-issuing each entry's operation. Under what circumstance does this go wrong, and what should the tool rely on instead?Replication internalssenior8+y07A hot endpoint runs `db.orders.find({"customer.id": id}, {_id: 0, "customer.id": 1, placedAt: 1, orderNumber: 1})` against an index on `{"customer.id": 1, placedAt: -1}`, expecting a covered query. `explain()` shows `totalDocsExamined` equal to the number of documents returned, not zero. Why isn't it covered, and what's the actual aggregation-pipeline cost of joining in the order's line items afterward?Aggregation and covered queriessenior8+y08A session-token collection needs to expire tokens automatically after 30 minutes of inactivity, and a separate `orders` query needs a fast, small index for `status = "OPEN"` orders only, out of a collection that's 99% `CLOSED`. What index type solves each, and why wouldn't a plain index work as well for either?Indexesmedium3-5y09A service writes an order with `w: "majority"` then immediately reads it back from a secondary with `readPreference: "secondary"` to confirm it was saved — and sometimes the read comes back empty. The write concern was already majority; why does the read still miss it, and what actually fixes it?Replication and consistencyhard5-8y10A revenue-by-SKU aggregation with `$group` works fine in staging on a sample collection, then fails in production with an 'exceeded memory limit' error once the collection crosses a certain size. What's actually happening, and is `allowDiskUse: true` the right fix?Aggregation pipelinemedium5-8y11A collection is sharded on `_id` (the default `ObjectId`) to spread write load across the cluster, but one shard's disk and CPU stay pinned near 100% while the others sit idle. Why does sharding on `_id` fail to spread inserts here, and what shard key would actually fix it?Shardingsenior8+y