Multi-Document Transactions
Starting from MongoDB 4.0, transactions allow you to perform several operations on multiple documents within a single transaction block. Transactions follow the ACID principles which means that if there is a failure, either all the operations within the transaction are fully committed or fully rolled back. This guarantees the consistency of the data.
session.startTransaction();
try {
// Perform multiple operations
session.commitTransaction();
} catch (error) {
session.abortTransaction();
throw error;
}
What is Atomicity in MongoDB and How to Achieve it?
Atomicity ensures that all changes within a single operation are either fully completed or not applied at all, preventing data corruption and inconsistencies.
Table of Content
- Atomicity and How to Achieve it
- Operation Example: insertOne()
- Success or Error
- Atomicity on the Document Level
- Including Embedded Documents
- Single Document Operations
- Multi-Document Transactions
Contact Us