Skip to content

MongoDB Transaction AQSA Spark By {Examples}

  • by

MongoDB transactions empower to combine several operations into a single atomic unit of work. Any operations contained within a transaction are guaranteed to either be successful or to have no impact, which ensures data consistency and integrity in multi-document operations.

To work with transactions in MongoDB, we need to make sure you are using a replica set or a shared cluster with MongoDB version 4.0 or later. Note that transactions are not available in standalone MongoDB deployments.

1. MongoDB transaction ACID properties

MongoDB transactions uphold the ACID characteristics. Let’s explore each of these properties in the context of MongoDB transactions.

Atomicity: MongoDB’s transactions are atomic, which means that if any one of the operations included within a transaction fails, the entire transaction is undone, leaving the database in its initial state.

Consistency: MongoDB ensures consistency by enforcing data validation rules, such as schema validation, during transactional operations.

Isolation: In MongoDB, transactions provide snapshot isolation, ensuring that every transaction gets an exact snapshot of the data initially. This isolation level avoids unclean devours, non-repeatable reads, and other concurrency-related problems.

Durability: When a transaction is committed in MongoDB, the changes are durably written to the storage layer and are replicated across the replica set to ensure data durability.

2. How to perform a transaction on MongoDB

To begin a transaction in MongoDB, we need to create a session. Sessions, which are used to carry out several activities inside a transaction, are connected to a particular client connection. The following command of the transaction enables us to create a session.

# Create Session
session = db.getMongo().startSession()

On execution, we get the MongoDB server connection object using getMongo() and then start a new session using startSession().

Next, by using the session object’s startTransaction method, we will start a transaction. This sets the session in a transaction state. To begin a transaction within a MongoDB session, issue the following command.

# Starting transaction
session.startTransaction()

Note that the transactional behavior only applies to operations executed within the session after the startTransaction() method is invoked. Any operations executed before starting the transaction or on separate sessions are not part of the transaction.

 From here, the transaction is started, we now can execute multiple read and write operations within the session, and they will be part of the transaction until it is explicitly committed or aborted. We have performed the update operation in the current transaction session, which updates the document of the specified collection with the given value of the field.

# Update Method
db.updateOne({name: ‘896721’},{$set:{fees:8000}})

The document has been modified with the new value of the field as represented by the output.  

However, our change to the collection will not be seen outside the transaction until committed. There, we can see the collection below same as before. 

We can either commit the transaction or abort it after performing the necessary task. Committing the transaction applies all the changes made within the transaction to the database atomically.

We viewed the collection, which is now updated with the new field value of the specified document.

2.1 Aborting transaction in MongoDB

Moreover, we can abort the transaction if we don’t want to alter the collection. Aborting the transaction discards all the changes made within the transaction. To cancel the transaction, apply the given command. Keep that in mind, and we can’t perform an abort operation after the commit operation.

# aborting transaction
session.abortTransaction()

The session leaves the transaction state whenever the transaction is finished by committing or aborting.

3. Conclusion

In conclusion, we have explored the transaction in MongoDB and provided an example to perform a transaction. Transactions in MongoDB provide a reliable way to ensure data integrity and consistency in complex operations.

 MongoDB transactions empower to combine several operations into a single atomic unit of work. Any operations contained within a transaction are guaranteed to either be successful or to have no impact, which ensures data consistency and integrity in multi-document operations. To work with transactions in MongoDB, we need to make sure you are using a  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *