0
0
MongoDBquery~10 mins

When transactions are necessary vs unnecessary in MongoDB - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a transaction in MongoDB.

MongoDB
const session = client.startSession();
session.[1]();
Drag options to blanks, or click blank then click option'
AstartTransaction
BcommitTransaction
CabortTransaction
DendSession
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitTransaction instead of startTransaction
Calling endSession before starting a transaction
2fill in blank
medium

Complete the code to commit a transaction after operations.

MongoDB
await session.[1]();
Drag options to blanks, or click blank then click option'
AstartTransaction
BabortTransaction
CcommitTransaction
DendSession
Attempts:
3 left
💡 Hint
Common Mistakes
Calling startTransaction again instead of commitTransaction
Using abortTransaction when you want to save changes
3fill in blank
hard

Fix the error in the code to abort a transaction properly.

MongoDB
try {
  await session.commitTransaction();
} catch (error) {
  await session.[1]();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
BabortTransaction
CcommitTransaction
DendSession
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to start a new transaction inside the catch block
Calling commitTransaction again instead of abortTransaction
4fill in blank
hard

Fill both blanks to create a transaction that updates two collections atomically.

MongoDB
const session = client.startSession();
try {
  session.[1]();
  await collection1.updateOne(filter1, update1, { session });
  await collection2.updateOne(filter2, update2, { session });
  await session.[2]();
} finally {
  await session.endSession();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
BcommitTransaction
CabortTransaction
DendSession
Attempts:
3 left
💡 Hint
Common Mistakes
Using abortTransaction instead of commitTransaction
Not starting the transaction before updates
5fill in blank
hard

Fill all three blanks to handle a transaction with error rollback and session cleanup.

MongoDB
const session = client.startSession();
try {
  session.[1]();
  await collection.updateOne(filter, update, { session });
  await session.[2]();
} catch (error) {
  await session.[3]();
} finally {
  await session.endSession();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
BcommitTransaction
CabortTransaction
DendSession
Attempts:
3 left
💡 Hint
Common Mistakes
Not aborting the transaction on error
Not starting the transaction before operations