Mongo 4.0 Transactions — Why? and How-to?

sayal adhikari
Nov 7 · 3 min read

Starting from the 4.0 release, MongoDB supports multi-document ACID transactions.

Actually MongoDB has always supported transactions in the form of single document transactions. MongoDB 4.0 extends these transactional guarantees across multiple documents, multiple statements, multiple collections, and multiple databases.

As this article is all about how-to? and Why? Let’s divide this into two parts for sake of ease. Now, before we move on to How-to? let’s learn Why?

WHY??

Transactions are not commonly used in mongodb as ACID transactions over multiple documents are not it’s strong suite. But there might come some time where one might need this power. And to sum this perfectly, here are two paragraphs from mongodb’s official documentation:

“In MongoDB, an operation on a single document is atomic. Because you can use embedded documents and arrays to capture relationships between data in a single document structure instead of normalizing across multiple documents and collections, this single-document atomicity obviates the need for multi-document transactions for many practical use cases.

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.”

Now that we know why? let’s focus on how-to?

HOW-TO??

There are few steps to follow before we dive into using transactions:

Step 1: Installation of Mongo 4.0

To install mongo in your system, please refer their official documentation which can be found here. Follow all the steps over there and you are good to go.

Step 2: Starting Mongo

  • You can use start-mongo.sh to run
  • When you are done, you can use stop-mongo.sh

The reason we are first starting mongo is to check our installation. And, for why we stopped mongo after just starting it, you need to be patient and move forward in this article.

Let me give you a hint: It has something to do with replica sets

Now that we have installed, started and stopped mongo, let’s move ahead and dive into the real deal.

Step 3: Starting Mongo Again???

Now to the part i told you to be patient about. Do you remember the hint i gave you? It had something to do with replica sets, right?

MongoDB currently supports transactions over a replica set.

yes, you read it right. Mongo 4.0 only supports transactions in replica set. So let’s create a replica set without any further due.

And for that, just copy-paste and run mongod --replSet rs0

After running the above command, our replica set has started. This is the most important part to keep in mind. We are now running mongo with replica set and this gives us the power to use transactions. As we’ve come to know that transactions are only supported over replica sets, running mongo normally will throw error while using transactions. So, keep in mind to run mongo with the above given command.

Finally, initiate replica set — if not already:

mongo --eval "rs.initiate()"

The code above does all the trick. This command initiates the replica set and need to be run only once. After running this command, you can work with mongo transaction in any of your projects. Here are some useful links for using mongo transactions on different frameworks and languages:

  1. Mongo Transactions with Spring Boot
  2. Java and MongoDB 4.0 Support for Multi-Document ACID Transactions
  3. MongoDB Transaction With Node JS
  4. Working with MongoDB Transactions with C# and the .NET Framework
  5. MongoDB official Documentation on Transactions

We’ve come to the end of this article and you must have many information amassed by know. Hopefully, this article helped you find your answer. This article is solely written to help people find all the necessary information about mongo transactions in one place. I too had to research a lot while using transactions and finally, with the push of a friend, decided to write this article to make some people’s work easier.

Have a good time coding.

Namaste

sayal adhikari

Written by

Experienced Software Engineer with a demonstrated history of working in the computer software industry.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade