Transaction in MongoDB

The transaction is defined as the property of MongoDB to operate (s) either entirely or entirely not.

The transaction's role is to tell MongoDB that all the operations will succeed together or fail together. If failed, then roll the database to the previous stable state.

Transactions were added in the MongoDB with version 4.0 requires a replica set, i.e. transactions are not possible without the web server or cloud server.

Without a transaction?

The above image contains the "user" collection with a user name and objectID.

Then there is another "post" collection with some documents as posts by the user in the user collection.

In this case, we want to delete both the user and the posts which resemble that user, but we want to make sure that both types of data get deleted either entirely or not.

  • To make this case happen, we can first delete the user from the user collections.
  • Store the objectID of that user.
  • Use the objectID of that user, and delete the posts which resemble that objectID in the post collection.

Now, the step mentioned above will work. But the problem here is that due to some reasons (server down, network failure etc.), we cannot delete the posts using the object of the user, which is already deleted.

This can cause a problem in the database, so the steps mentioned above to delete the data atomically are not foolproof. This is where the role of transaction comes up.

With a transaction

It tells MongoDB that all the operations will either succeed together or fail together. If operational, the database must be restored to the previous stable state.

How does the transaction work?

For the transaction, a session is needed.

Session means a logical grouping of our requests to the server. A session object is always required because every command we send goes to the server, and the server forgets the user. So we need a session to be always recognizable by the server.

const session = db.getMongo().startSession()

With this, a session object is created and can be used to group the requests by starting the transaction.

session.startTransaction()

The above command is written to start the transaction.

NOTE: The command, as mentioned above, is necessary to run before making any changes in the database in case you want to use the transaction functionality in MongoDB. If the transaction is not started, all your changes will be directly reflected in the database.

This session object is used to access the collections

const usercoll = session.geodatabase("<name_of_database>").userconst postcoll = session.geodatabase("<name_of_database>").post

Here, two collections inside the database are accessed using the session. Now all the operations can be performed on collection using these constants or pointers.

If the user is deleted using "user call", it will get deleted, but the changes will not be reflected in the server immediately.

The user will be present in the database if we search using the find (). To reflect these changes in the database, you can either commit the transaction or, if you want to close the session, you can abort it too.

session.commit transaction ()
OR
session.abort transaction ()

NOTE: Command to abort the session cannot write after the commit command.