Course contentsShow
← Node.js
Lesson 848 of 1,9808. Data Persistence and ORM IntegrationFree lesson

NoSQL Transactions Across Collections

You will manage multi-collection operations requiring strict consistency.

In this lesson, you’ll learn how to maintain strict data consistency when your business logic requires updating multiple collections simultaneously in a NoSQL database.

The Challenge of Multi-Collection Operations

In a relational database, "ACID" transactions are the default, ensuring that if one part of a sequence fails, everything rolls back. In NoSQL systems like MongoDB, documents are often designed for independence. However, some workflows—such as transferring a balance between two distinct user accounts stored in different collections—demand that either both operations succeed or neither happens at all. Without transactions, a system crash between two write operations could leave your data in a corrupt, inconsistent state.

Using Transactions for Consistency

To handle these scenarios, we use a session-based transaction wrapper. Think of this as creating a "temporary sandbox" for your database operations. If every operation inside that sandbox completes, you "commit" the changes to the permanent store. If any single operation fails, you "abort" the transaction, effectively reverting the database to the exact state it was in before you started.

Here is how you might structure this in a Node.js environment:

const session = await db.startSession();
session.startTransaction();

try {
  // Perform operations across multiple collections
  await db.collection('accounts').updateOne(
    { userId: 'A' }, { $inc: { balance: -100 } }, { session }
  );
  
  await db.collection('ledgers').insertOne(
    { transactionId: 'TXN123', amount: 100 }, { session }
  );

  // If both succeed, commit the changes
  await session.commitTransaction();
} catch (error) {
  // If anything fails, discard all changes
  await session.abortTransaction();
  throw error;
} finally {
  session.endSession();
}

Why This Matters

While NoSQL prioritizes speed and scalability by decoupling data, transactions allow us to "opt-in" to strict consistency when the business logic absolutely requires it. By explicitly passing the session object to your database commands, you ensure that the driver groups these actions together, preventing partial writes that could break your application's integrity.

Key Takeaway: Use session-based transactions to group multi-collection operations into an all-or-nothing unit, ensuring your NoSQL data remains consistent even during unexpected system failures.