Question 42 / 50
You must move money from account A to account B. Would you use a MongoDB transaction? What else would you consider?
Short answer
If balances live in two documents, yes — a transaction (or a ledger of immutable entries plus a projection) so you never show a withdraw without a deposit. If you can model a double-entry ledger as inserts of transaction documents, you might avoid updating two balance documents at all.
Why?
Updating two balance fields is the classic multi-document need. An append-only ledger — insert a transfer document, derive balances — often fits MongoDB better than mutable balances, and inserts are single-document atomic. If you do mutate two accounts, use a transaction, idempotency keys, and retries on transient errors. Do not use a transaction for 'every financial-looking write' such as logging an audit line on the same document as the balance.
Interview tip
Offering the ledger alternative shows modelling skill, not just 'yes, transactions exist'.
Common mistake
Either 'never use transactions in MongoDB' or 'use them for every payment-related write'.
How did you do?
Cheat Sheet