0
0
MongoDBquery~10 mins

Why transactions are needed in MongoDB - Test Your Understanding

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
BbeginTransaction
CopenTransaction
DinitTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'beginTransaction' instead of 'startTransaction'.
Trying to call 'openTransaction' which does not exist.
2fill in blank
medium

Complete the code to commit a transaction in MongoDB.

MongoDB
await session.[1]();
Drag options to blanks, or click blank then click option'
AcommitTransaction
BsaveTransaction
Ccommit
DfinalizeTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'commit' instead of 'commitTransaction'.
Trying to use 'saveTransaction' which is not a valid method.
3fill in blank
hard

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

MongoDB
await session.[1]();
Drag options to blanks, or click blank then click option'
ArollbackTransaction
BabortTransaction
CcancelTransaction
DstopTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rollbackTransaction' which is not a MongoDB method.
Trying 'cancelTransaction' or 'stopTransaction' which do not exist.
4fill in blank
hard

Fill both blanks to correctly use a transaction with a session in MongoDB.

MongoDB
const session = client.startSession();
try {
  session.[1]();
  await collection.insertOne(doc, { session: [2] });
  await session.commitTransaction();
} finally {
  await session.endSession();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
Bsession
CcommitTransaction
DabortTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'commitTransaction' in the first blank instead of 'startTransaction'.
Passing something other than 'session' in the operation options.
5fill in blank
hard

Fill all three blanks to handle a transaction with error handling in MongoDB.

MongoDB
const session = client.startSession();
try {
  session.[1]();
  await collection.updateOne(filter, update, { session: [2] });
  await session.[3]();
} catch (error) {
  await session.abortTransaction();
} finally {
  await session.endSession();
}
Drag options to blanks, or click blank then click option'
AstartTransaction
Bsession
CcommitTransaction
DabortTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'abortTransaction' in the third blank instead of 'commitTransaction'.
Not passing the session object in the operation options.