0
0
MongoDBquery~10 mins

Multi-document transactions in MongoDB - Interactive Code 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 session in MongoDB.

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

Complete the code to commit the 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
Using abortTransaction instead of commitTransaction.
Calling startTransaction again instead of committing.
3fill in blank
hard

Fix the error in the code to abort the transaction on failure.

MongoDB
try {
  await session.startTransaction();
  // operations
  await session.commitTransaction();
} catch (error) {
  await session.[1]();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
BcommitTransaction
CendSession
DabortTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Calling commitTransaction in the catch block.
Calling startTransaction again inside catch.
4fill in blank
hard

Fill both blanks to run operations inside a transaction session.

MongoDB
const session = client.startSession();
try {
  await session.[1]();
  await collection.[2](doc, { session });
  await session.commitTransaction();
} finally {
  await session.endSession();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
BinsertOne
CupdateOne
DabortTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using updateOne instead of insertOne for inserting.
Not starting the transaction before operations.
5fill in blank
hard

Fill all three blanks to update two documents atomically in a transaction.

MongoDB
const session = client.startSession();
try {
  await session.[1]();
  await collection1.[2]({ _id: 1 }, { $set: { status: 'done' } }, { session });
  await collection2.[3]({ _id: 2 }, { $inc: { count: 1 } }, { session });
  await session.commitTransaction();
} catch (e) {
  await session.abortTransaction();
} finally {
  await session.endSession();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
BupdateOne
DinsertOne
Attempts:
3 left
💡 Hint
Common Mistakes
Using insertOne instead of updateOne for updates.
Not starting the transaction before updates.