Embed when the child belongs to the parent and is read together, like the addresses of a user. Reference when the child is shared, huge or grows without limit, like a user’s million log entries.
Say it like this in the interview
“Design around access patterns. Embed one-to-few data read together, which gives one read and atomic updates. Reference for one-to-many unbounded data or shared entities, remembering the 16MB document limit. Patterns like the subset pattern keep the latest N items embedded.”
Traps that make people fail
Embedding an array that grows forever, like all comments on a viral post.
Yes. The database won’t force one, but your app still expects a certain shape. Without rules, bad data creeps in.
Say it like this in the interview
“Use JSON Schema validation on collections and ODM schemas like Mongoose or Zod at the app level. Flexible schema helps with evolution, but consistency still matters.”
“$match first so it can use an index and reduce data early, $group by month with $sum, then $sort. $lookup acts like a join, but if I need it constantly, I reconsider the schema.”
Same idea as SQL indexes. For compound indexes, order fields as Equality, Sort, Range: exact matches first, then sort field, then range filters.
Say it like this in the interview
“B-tree indexes on fields, compound indexes following Equality-Sort-Range, verified with explain('executionStats') to see IXSCAN vs COLLSCAN and keys examined vs returned. TTL indexes for expiring data and unique indexes for constraints.”
Yes, multi-document transactions exist on replica sets, but single-document updates are already atomic, so good schema design often avoids needing them.
Say it like this in the interview
“Single-document operations are atomic. Multi-document ACID transactions are supported on replica sets and sharded clusters, but they cost performance, so I design schemas so most invariants fit in one document and use transactions where truly needed, like transfers.”
Splitting data across several servers by a key. A bad key sends all traffic to one server.
Say it like this in the interview
“Sharding partitions data across shards by a shard key. A good key has high cardinality, even distribution and matches common queries; monotonically increasing keys like timestamps create hot spots unless hashed.”