Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitTransaction instead of startTransaction.
Calling endSession before starting the transaction.
✗ Incorrect
The startTransaction() method begins a transaction in the session.
2fill in blank
mediumComplete the code to commit the transaction after operations.
MongoDB
await session.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using abortTransaction instead of commitTransaction.
Calling startTransaction again instead of committing.
✗ Incorrect
The commitTransaction() method saves all changes made during the transaction.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling commitTransaction in the catch block.
Calling startTransaction again inside catch.
✗ Incorrect
On error, abortTransaction() cancels all changes made in the transaction.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using updateOne instead of insertOne for inserting.
Not starting the transaction before operations.
✗ Incorrect
Start the transaction with startTransaction() and perform the insert with insertOne() inside the session.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using insertOne instead of updateOne for updates.
Not starting the transaction before updates.
✗ Incorrect
Start the transaction, then update documents in both collections using updateOne() inside the session.