WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Advanced · question 45 of 100

How do you handle transactions in MongoDB, and what are the limitations?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

MongoDB supports multi-document transactions starting from version 4.0. With transactions, users can perform multiple insert, update, or delete operations on multiple collections and have them either all succeed or all fail, preserving the integrity of the data. Transactions are commonly used in scenarios that involve multiple databases or multiple collections within a database. Transactions can be managed using the following objects in MongoDB:

1. ‘ClientSession‘: A session object that MongoDB uses to store transaction-specific data, such as transaction ID.

2. ‘TransactionOptions‘: An object that sets transaction options, such as read concern and write concern.

3. ‘Transaction‘: An object that specifies the sequence of database operations to be performed within a transaction.

To execute a transaction in MongoDB, you need to follow these steps:

1. Begin a session.

2. Start a transaction using the session.

3. Perform multiple read and write operations within the transaction using the session.

4. Commit or abort the transaction.

Here is an example code snippet that demonstrates how to use transactions in MongoDB:

// Begin a session
session = client.startSession();
session.startTransaction();

try {
   // Read and write operations within the transaction
   session.getDatabase("db1").getCollection("coll1").insertOne(new Document("_id", "doc1"));
   session.getDatabase("db2").getCollection("coll2").updateOne(new Document("_id", "doc2"), 
   new Document("$set", new Document("field1", "value1")));

   // Commit the transaction
   session.commitTransaction();
} catch (RuntimeException e) {
   // Abort the transaction if there is an error
   session.abortTransaction();
} finally {
   // End the session
   session.close();
}

However, there are some limitations that come with using Mongo transactions. These limitations are:

1. Distributed transactions cannot be performed across shards in a MongoDB cluster.

2. The transaction cannot operate on more than 1000 documents.

3. MongoDB does not support nested transactions.

4. Transactions increase the load on the server and may affect performance when they are used excessively.

5. Transactions cannot be used with certain write operations such as ‘geoNear‘.

In conclusion, MongoDB supports transactions to ensure data integrity when performing multiple read and write operations. However, there are certain limitations that come with using transactions, such as performance, scalability, and the inability to perform distributed transactions across shards.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics