Q1
Why does db.orders.find().sort({ createdAt: -1 }).skip(50000).limit(20) fail with memory errors on large unindexed collections?
MongoDB caps in-memory blocking sorts at 100 MB RAM before throwing Sort exceeded memory limit. Creating a matching index on { createdAt: -1 } lets MongoDB return pre-sorted keys incrementally without sorting in RAM.
Q2
How does MongoDB's ESR (Equality, Sort, Range) rule determine optimal compound index column order?
Place exact Equality fields (status: 1) first, Sort fields (createdAt: -1) second, and Range filters (price: { $gte: 100 }) last—otherwise range scans prevent index-ordered sorting!
Q3
Why can querying a single index field with $ne or leading-wildcard regex (/abc/) trigger slow full-index or collection scans?
Negations ($ne, $nin) match almost the whole dataset, and non-anchored regex (/term/) cannot use B-tree prefix ordering. Only prefix-anchored ^term or dedicated Atlas Search / text indexes use fast index lookups.
Q4
How does a Covered Query achieve maximum read throughput in MongoDB?
When every field in both the filter and projection ({ _id: 0, email: 1 }) exists inside the B-tree index keys, MongoDB answers directly from RAM index entries (totalDocsExamined: 0) without touching disk documents!
Q5
What subtle bug arises when indexing two array fields in the exact same compound index ({ tags: 1, categories: 1 })?
MongoDB rejects creating a compound multikey index with more than one array field (cannot index parallel arrays) because indexing the Cartesian pairs (m * n) causes explosive index bloat.
Q6
When choosing Embedding vs Referencing in MongoDB schema design, what three signals mandate Referencing?
Move to referenced collections whenever child arrays grow unboundedly (approaching the 16 MB BSON limit), when child items need independent pagination/updates across parents, or in many-to-many shared relations.
Q7
Why does { writeConcern: { w: 'majority' } } + { readConcern: 'majority' } prevent dirty reads across replica set failovers?
Writing with w: 1 acknowledges before secondaries replicate—losing committed data if primary crashes seconds later. majority guarantees durability across quorum nodes so rolled-back dirty writes are never exposed.
Q8
How does $facet inside Aggregation Pipelines work, and what memory trap should you watch out for?
$facet runs multiple independent sub-pipelines (like items + total count + brand counts) in one pass—however, the combined $facet output still becomes ONE document constrained by the 16 MB BSON limit.
Q9
Why do TTL (Time-To-Live) indexes delete expired documents asynchronously ~60 seconds late rather than at the exact millisecond?
MongoDB's background TTL monitor thread wakes roughly once every 60 seconds and only indexes single Date fields—never rely on TTL precision for strict security token expiry without checking expiresAt > now in queries.
Q10
How can Shard Key choice (hashed vs monotonically increasing _id / timestamp) cause a single-shard hot spot?
Sharding on sequential createdAt or ObjectId directs 100% of new inserts onto the single rightmost chunk/shard! Use hashed shard keys or compound tenant+timestamp keys to distribute writes evenly across shards.