{"success":true,"name":"guard-against-unbounded-arrays-in-mongodb-documents","description":"A MongoDB document has an array that keeps growing","category":"ai-brimind","path":"ai-brimind/guard-against-unbounded-arrays-in-mongodb-documents/SKILL.md","file":"SKILL.md","content":"---\nname: guard-against-unbounded-arrays-in-mongodb-documents\ndescription: \"A MongoDB document has an array that keeps growing\"\n---\n\n# Guard Against Unbounded Arrays in MongoDB Documents\n\n## When to use\nProject uses MongoDB, and a schema contains an array field with no natural size limit (logs, messages, history, events).\n\n## Example request\nPushing every login event into user.history → grows forever, heads toward the 16MB limit; events belong in their own collection. Chat document with all messages embedded → each new message rewrites a bigger doc; the bucket pattern (one doc per day/100 messages) fixes it. Keeping 'last 10 viewed items' → bounded on purpose: $push with $slice keeps it capped, embedding is fine. Each maps to 'bounded array = fine, unbounded array = separate collection or bucket pattern'.\n\n## Instructions\nAct as a schema reviewer who flags every array field and asks: what is its maximum size in two years? For each array in a MongoDB document: (1) classify it as BOUNDED (a real, small maximum exists — tags, last-N items) or UNBOUNDED (grows with usage — logs, messages, history), (2) for bounded arrays, embedding is fine; enforce the bound in code with $push + $slice so it cannot silently grow, (3) for unbounded arrays, do not embed: move the items to their own collection with a parent reference, or apply the bucket pattern (group items into fixed-size documents) if they are always read in chunks. Remember the physical reasons: documents cap at 16MB, and every update rewrites the document, so fat documents make everything slower. If growth is unknown, ask about expected volume before choosing. Only keep an embedded array once its bound is confirmed and enforced, and explain why the structure survives growth, not just today's small dataset.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1045\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}