Document modellingeasy0-2 years

A `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?

The embed-versus-reference decision depends on whether the array is bounded, and an orders array on a customer is not — a customer who places one order a week for five years has hundreds of them, with no ceiling. As it grows, every read of the customer's profile pulls the whole order history along whether the page needs it or not, every new order rewrites the entire customer document, and the document creeps toward the size where growth gets expensive and, eventually, toward the hard 16 MB limit. The fix is to make orders its own collection with a customerId reference and an index on it, so reading a customer's profile touches one small document and reading their orders is a separate, paginated query — exactly the embed/reference table's rule: reference what's unbounded, embed what's bounded and read with its parent.

The lesson behind it →