Video Transcript (English)
In this video, we're going to learn how to use multi-document transactions in MongoDB. We'll use the shell to complete a multi-document transaction as well as abort a multi-document transaction.
It's highly likely that you'll use transactions inside of one of MongoDB's drivers instead of here in the shell. While the language and the syntax will be a little bit different, the steps will be very similar to what you'll use in this video.
In this scenario, we'll create a transaction for the transfer of funds from one customer account to another's. Let's go ahead and take a look at our first account.
This will be account ending in 6066 for Naja Petersen And we can see that they have an account balance of 2891. Let's then go and take a look at our second account that we'll be working with today.
There's going to be account 4500. And this person, Florence Taylor, also has an account balance of 2598.
We're going to go ahead and make a transaction from Naja to Taylor in the amount of about $30. To begin, we'll need to open a session in the shell.
A session is a group of database operations that are related to each other and should be run together, similar to a transaction. Before we start our transaction, it's important to note that by default, a transaction has a maximum runtime of one minute.
In other words, once you make the first write in your transaction, you've only got 60 seconds to complete it. If we get an error message that resembles something like Mongo server error, transaction one has been aborted, it's likely that the transaction has timed out and needs to be run again.
So we need to make sure that we have our database operations prepared beforehand in order to meet this constraint. Let's go ahead and create our session in the Mongo shell. I'm going to create a constant for the session so that we're creating a variable. And we're going to use the DB object to getMongo.
This method is going to allow us to access the Mongo session. And we'll go ahead and start a new session here. Once we've created our session, we can go ahead and start our transaction. Notice that there's no shell output when we use the start transaction method.
This is completely expected, and is simply how we start creating a transaction during this particular session. Next, we'll go ahead and create another variable for an account object.
This account variable is going to serve as a reference to the account's collection. This will make it easier to write the subsequent queries.
This variable will be created by using our session object, grabbing the specific database that we want to work with using the getDatabase method, and then using the getCollection method to access the specific collection.
In this case, we're going to getDatabase on bank and then getCollection on the accounts collection. Next, we'll go ahead and create our first update.
In this case, we're going to be subtracting $30 from the account ending in 6066 for Naja Petersen.
In this case, we'll use the updateOne. And remember that for updateOne, we need to pass both a query document and a mutation document in order for this method to run.
In this case, for the query document, we'll pass the account ID ending in 6066, and then for the mutation document, we'll pass an increase operator for the balance field for a value of negative 30.
After running this query, we can see that we have a modifiedCount of 1.
This shows us that this particular operation has been conducted on the database.
However, in the event that this transaction fails, this operation can be reverted back to its original state. Next, let's go ahead and update our other account for which we're going to increase the value of $30. We'll go ahead and update our account for Florence Taylor ending in 4500.
In this case, we'll still use the updateOne method. But we're going to change the query document to end in 4500 and the mutation document to instead be a positive 30 instead of a negative 30.
If you get an error at this point, it's likely because you've taken too long with your transaction. In MongoDB, all transactions have to execute within 60 seconds of the first write.
In this case, feel free to use session.startTransaction again, and create your updateOne methods exactly the same way as you did before. From here, we'll go ahead and commit our transaction to the database. To do this, we'll use a session.commitTransaction method.
And in this case, this will make sure that the operations that we've conducted during the transaction are all committed at the same time. Using our commitTransaction, we'll complete our transfer of $30 from the account ending in 6066 to our account ending in 4500.
In order to verify that our transactions have been successful, we can use the find method to take a look at the documents post transaction.
First, let's take a look at account ending in 6066 for Naja. We can see that the balance has been updated to 2861 from its original value of 2891. For our second account, we can see that Florence Taylor's account balance has been now updated to 2628. This worked well.
But what if we decide that we don't want to commit a transaction for some reason? We can use the abortTransaction method before committing a transaction to roll the database back to its original state.
We can utilize the session object that we've already used before to start a new transaction. We can also utilize the same account variable from before in order to reference the accounts collection.
In this case, we'll go ahead and update the Florence Taylor account by increasing it by another $5.00. To do so, we'll utilize the updateOne method. And again, we can see that our modifiedCount is still 1. But during our transaction, we need to cancel the entire process.
In this case, we can use session.abortTransaction. AbortTransaction, similar to startTransaction, doesn't have any specific feedback that's brought back to the shell.
However, it does cancel the transaction mid-flight. In this case, when we go to look at the account ending in 4500, we'll see that the account value has been retained.
By using a query with the find method, we can see that the account for Florence Taylor still has a balance of 2628. Great job. Here are some key takeaways from this video. When using multi-document transactions, we must first start a session using the startSession method.
The startTransaction and commitTransaction methods start and complete a transaction in a session. If you want to stop a transaction and roll the database back to its original state, you can use the abortTransaction method.