{"success":true,"name":"choose-embed-vs-reference-in-mongodb-deliberately","description":"Modeling a MongoDB relation and unsure whether to embed or reference","category":"ai-brimind","path":"ai-brimind/choose-embed-vs-reference-in-mongodb-deliberately/SKILL.md","file":"SKILL.md","content":"---\nname: choose-embed-vs-reference-in-mongodb-deliberately\ndescription: \"Modeling a MongoDB relation and unsure whether to embed or reference\"\n---\n\n# Choose Embed vs Reference in MongoDB Deliberately\n\n## When to use\nProject uses MongoDB, and you are about to store related data (children, items, comments) inside or outside a document.\n\n## Example request\nOrder with its line items → always read together, bounded size: embed. Post with millions of comments → unbounded growth: reference, comments get their own collection. User referenced by many features → shared, updated independently: reference with ObjectId. Product with a fixed list of image URLs → small, owned, read together: embed. Each case maps to the classic 'access together + bounded = embed' rule.\n\n## Instructions\nAct as a MongoDB modeler who never embeds or references by habit — only by rule. Before choosing, answer three questions out loud: (1) Is the child data always read WITH the parent? (2) Is its size BOUNDED (a known small maximum)? (3) Is it updated independently or shared by other documents? Apply the rule: read-together + bounded + owned → embed; unbounded, shared, or independently updated → reference (store the ObjectId, or use the extended-reference pattern to copy a few display fields). Name the pattern you chose. Watch for the classic trap: an array that grows forever inside one document will hit the 16MB document limit and slow every read — that is a referencing case, not an embedding case. If you cannot answer the three questions from the spec, ask before modeling. Only implement once the choice is confirmed, and explain why it matches the real access pattern, not just what felt simpler to code.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1042\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}