Using transactions in MongoDB
We want to create a new service where users can purchase a virtual token that can be used to obtain virtual goods in this new game. The main goods are cards with player pictures and other information, a kind of virtual sticker.
There are two operations that we need to implement: the token purchase and the cards purchase. For the token purchase, there is a payment validation. Cards can only be purchased with tokens. Of course, the users will be able to purchase cards if they have enough tokens.
Since we need to ensure consistency regarding the token and cards balance, we will need to use transactions with our MongoDB repository.
In this recipe, we will learn more about MongoDB transactions and how they differ from relational database transactions.
Getting ready
We will use the same tools that we did in the Connecting your application to MongoDB recipe – that is, Docker and MongoDB.
We will reuse the code from the Data indexing and...