MongoDB Documentation, Release 3.0.4 Step 3: Apply the transaction to both accounts. Apply the transaction t to both accounts using the update() method if the transaction has not been applied to the accounts. In the update condition, include the condition pendingTransactions: { $ne: t._id } in order to avoid re-applying the transaction if the step is run more than once. To apply the transaction to the account, update both the balance field and the pendingTransactions field. Update the source account, subtracting from its balance the transaction value and adding to its pendingTransactions array the transaction _id . Upon successful update, the method returns a WriteResult() object with nMatched and nModified set to 1 . Update the destination account, adding to its balance the transaction value and adding to its pendingTransactions array the transaction _id . Upon successful update, the method returns a WriteResult() object with nMatched and nModified set to 1 . Step 4: Update transaction state to applied. Use the following update() operation to set the transaction’s state to applied and update the lastModified field: Upon successful update, the method returns a WriteResult() object with nMatched and nModified set to 1 . Step 5: Update both accounts’ list of pending transactions. Remove the applied transaction _id from the pendingTransactions array for both accounts. Update the source account. Upon successful update, the method returns a WriteResult() object with nMatched and nModified set to 1 . Update the destination account. Upon successful update, the method returns a WriteResult() object with nMatched and nModified set to 1 . Chapter 3. MongoDB CRUD Operations 118 db.accounts.update( { _id: t.source, pendingTransactions: { $ne: t._id } }, { $inc: { balance: -t.value }, $push: { pendingTransactions: t._id } } ) db.accounts.update( { _id: t.destination, pendingTransactions: { $ne: t._id } }, { $inc: { balance: t.value }, $push: { pendingTransactions: t._id } } ) db.transactions.update( { _id: t._id, state: "pending" }, { $set: { state: "applied" }, $currentDate: { lastModified: true } } ) db.accounts.update( { _id: t.source, pendingTransactions: t._id }, { $pull: { pendingTransactions: t._id } } ) db.accounts.update( { _id: t.destination, pendingTransactions: t._id }, { $pull: { pendingTransactions: t._id } } )