{"success":true,"name":"use-mongodb-transactions-only-where-document-atomicity-ends","description":"A MongoDB change must update several documents together or none","category":"ai-brimind","path":"ai-brimind/use-mongodb-transactions-only-where-document-atomicity-ends/SKILL.md","file":"SKILL.md","content":"---\nname: use-mongodb-transactions-only-where-document-atomicity-ends\ndescription: \"A MongoDB change must update several documents together or none\"\n---\n\n# Use MongoDB Transactions Only Where Document Atomicity Ends\n\n## When to use\nProject uses MongoDB, and one business action writes to multiple documents or collections that must stay consistent.\n\n## Example request\nTransferring points between two user docs → two writes must both succeed: a session transaction is the right tool. Order + its items → if they were modeled as ONE document, a single write is already atomic; the schema may fix what the transaction patches. Updating a doc plus a counters cache → decide: transaction, or accept eventual consistency and document it. Wrapping every single-doc update in a transaction 'for safety' → single-document writes are already atomic in MongoDB; that transaction is pure overhead. Each maps to 'first try to model atomicity into one document; transact only across real boundaries'.\n\n## Instructions\nAct as a consistency designer who knows MongoDB's first transaction tool is the document itself. When one action touches multiple documents: (1) ask first whether the schema could make it ONE document — single-document writes are always atomic, and needing multi-document transactions constantly is often a sign the data was split relationally, (2) if the writes genuinely cross documents/collections (transfers, cross-entity updates), use a session with startTransaction/commit, keep the transaction short, and handle abort/retry, (3) if perfect consistency is not actually required, say so explicitly and choose eventual consistency deliberately instead of accidentally. Never wrap single-document updates in transactions for comfort. If the business rule ('must these always match exactly?') is unclear, ask before choosing. Only implement once the consistency requirement is confirmed, and explain why the chosen mechanism (document atomicity, transaction, or accepted eventual consistency) matches the real business need.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1049\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}