{"success":true,"name":"embed-or-reference-in-mongodb-decide-from-how-you-read-not-h","description":"Designing MongoDB schemas and unsure whether data goes inside or in its own collection","category":"ai-brimind","path":"ai-brimind/embed-or-reference-in-mongodb-decide-from-how-you-read-not-h/SKILL.md","file":"SKILL.md","content":"---\nname: embed-or-reference-in-mongodb-decide-from-how-you-read-not-h\ndescription: \"Designing MongoDB schemas and unsure whether data goes inside or in its own collection\"\n---\n\n# Embed or Reference in MongoDB: Decide From How You Read, Not How It Feels\n\n## When to use\nProject uses MongoDB/Mongoose, and you are designing (or about to grow) a schema where related data could either live INSIDE the parent document (embedded array/object) or in its OWN collection linked by ID — and the choice is being made by gut feeling.\n\n## Example request\nClub members embedded as an array inside the club document → fine at 30 members, but the array grows without limit and every member update rewrites a huge document; unbounded growth is the classic signal to REFERENCE instead. An order's shipping address embedded in the order → perfect: it is a snapshot that must never change even if the user later edits their profile address; data that belongs to the moment should be embedded as a copy. Comments referenced in a separate collection, loaded with the post → if they are ALWAYS read together and capped in number, embedding would have saved a query; 'read together, bounded size' is the embed signal. User referenced by ID from ten collections → correct: entities with their own life, queried independently, are references — this whole decision is the named 'embed vs reference' trade-off, MongoDB's version of schema design.\n\n## Instructions\nAct as a data modeler who knows MongoDB's superpower and trap are the same feature — documents can nest anything, so the schema will happily let you build shapes that die at scale — and that the deciding question is never 'what feels tidy' but 'HOW will this data be read, written, and grown?'. For each relationship in your schema, do this: (1) Interrogate the access pattern out loud: Is the child data always loaded WITH the parent, or queried on its own? Updated rarely or constantly? And the killer question — is its quantity BOUNDED (an address, a settings object, a handful of tags) or UNBOUNDED (members, messages, transactions that grow forever)? (2) Apply the two honest rules: EMBED when the child is bounded, owned by exactly this parent, and read together with it — you win reads with zero joins and atomic updates of the whole unit. REFERENCE (own collection + parent ID field) when the data is unbounded, shared across parents, or queried independently — unbounded embedded arrays are the number one MongoDB self-inflicted wound: documents balloon toward the 16MB limit, every push rewrites more bytes, and you can never paginate an array the way you paginate a collection. (3) Name the snapshot exception explicitly: sometimes you embed a COPY of referenced data on purpose (the product name and price inside an order line) because history must not change when the source changes later — that duplication is not a mistake, it is time-freezing, but say so in a comment so the next reader knows it is intentional, and reconcile it with your one-source-of-truth discipline: the source is 'the price AT purchase time', which lives nowhere else. (4) For references, plan the read side in the same breath: which queries will need the joined data, and will populate/$lookup serve them — and remember your indexing discipline: the child's parent-ID field is the field every lookup filters on, so it is an index candidate from day one. (5) When torn between the two, prototype the top three queries of the feature against both shapes on paper — the shape that answers them in one or two clean, paginable queries wins; the shape that needs array-slicing gymnastics or fetch-everything-and-filter loses. Only commit the schema once you can state, for every relationship: bounded or unbounded, read together or apart, live link or frozen snapshot — and why the chosen shape stays sane when the data is a thousand times bigger.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #217\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}