In this we will discuss about how to perform two phase commit in MongoDB.
Consider a example where funds will be transferred from one account to another. In a relation database approach we just subtract fund from account a and add same amount to another account. To complete this task in mongoDB the concept of two phase commit will perform as shown in below example.
The examples use following two collections:
1. A collection named accounts to store account information.
2. A collection named transactions to store information on the fund transfer transactions.
Initialize collection with A and B Accounts
Below is mongo code for insert document in accounts collection.