{"success":true,"name":"keep-mongodb-lookup-rare-heavy-joins-signal-a-modeling-probl","description":"A MongoDB aggregation is chaining several $lookup joins","category":"ai-brimind","path":"ai-brimind/keep-mongodb-lookup-rare-heavy-joins-signal-a-modeling-probl/SKILL.md","file":"SKILL.md","content":"---\nname: keep-mongodb-lookup-rare-heavy-joins-signal-a-modeling-probl\ndescription: \"A MongoDB aggregation is chaining several $lookup joins\"\n---\n\n# Keep MongoDB $lookup Rare — Heavy Joins Signal a Modeling Problem\n\n## When to use\nProject uses MongoDB, and queries need multiple $lookup stages, or a $lookup runs on a hot, high-traffic path.\n\n## Example request\nFour $lookups to render one screen → the schema was designed relationally; the read pattern wants one document shape. $lookup on every feed item request → the extended reference pattern (copy the 2-3 display fields) removes the join from the hot path. $lookup joining on an unindexed foreign field → each input doc triggers a scan; the joined field needs an index. Occasional admin report with $lookup → perfectly fine; joins are for cold paths, not per-request rendering. Each maps to 'in MongoDB, frequent joins are a schema smell — the fix is modeling, not faster joins'.\n\n## Instructions\nAct as a data modeler who reads every $lookup as a question: 'why doesn't the document already contain what this screen needs?' When aggregations accumulate $lookup stages or a join sits on a hot path: (1) classify the path — cold (reports, admin, occasional) where $lookup is legitimate, or hot (per-request rendering, feeds, lists) where repeated joins are the relational habit leaking in, (2) for hot paths, apply the named NoSQL remedies before optimizing the join: embed if the data is owned and bounded; use the extended reference pattern (duplicate the few display fields, with a declared refresh strategy) if only a slice is needed; precompute with the computed pattern if it's an aggregate, (3) when $lookup stays, make it honest: index the foreignField, place $match before the lookup to shrink the input set, and $project early to join less data, (4) present the trade-off explicitly: duplication with a refresh contract versus join cost on every read — that's a decision, not a default. If read frequency is unknown, ask, because it decides everything. Only finalize once the join's presence on that path is justified or removed by remodeling, and explain why the read path now matches how a document database wants to be read.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1152\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}