Courseiva

C100DEV Drivers, Tools, Transactions, and Search Practice Question

A developer is using the MongoDB Node.js driver to perform a multi-document transaction that updates a document in the 'orders' collection and inserts a record into the 'audit' collection. After starting the session and transaction, the update succeeds, but the insert fails due to a duplicate key error. What should the developer do to handle this error and ensure data consistency?

⚠ Common exam trap

The trap here is thinking that a failed operation in a transaction can be retried in place, but the transaction must be aborted first.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Call session.abortTransaction() to roll back the update, then retry the entire transaction if appropriate.

In a multi-document transaction, if any operation fails, the entire transaction must be aborted to maintain atomicity. The correct action is to call abortTransaction() to roll back all changes made within the transaction. Committing or ignoring the error would lead to inconsistent data. Ending the session does not explicitly abort. Retrying without aborting is futile.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Retry the insert operation within the same transaction without aborting, hoping the duplicate key error resolves.

    Why it's wrong here

    Retrying the same insert within the same transaction will fail again because the duplicate key error is due to a constraint violation, not a transient issue. The transaction is in an error state and cannot continue. The only correct action is to abort the transaction and then decide whether to retry the whole transaction after addressing the root cause.

  • ✗

    Call session.commitTransaction() to commit the successful update and ignore the failed insert.

    Why it's wrong here

    Committing the transaction after an operation has failed would persist the update to 'orders' but not the insert to 'audit', violating the atomicity of the transaction. The transaction is designed to be all-or-nothing; committing a partially failed transaction is not allowed and would likely throw an error. This approach would lead to inconsistent data.

  • ✓

    Call session.abortTransaction() to roll back the update, then retry the entire transaction if appropriate.

    Why this is correct

    When an operation within a transaction fails, the transaction must be aborted to undo any changes made before the failure. Calling abortTransaction() ensures the update to 'orders' is rolled back, maintaining atomicity. After aborting, the developer can decide whether to retry the transaction, possibly after handling the duplicate key error (e.g., by checking if the audit record already exists). This is the correct way to handle errors in a transaction.

  • ✗

    Call session.endSession() to close the session and automatically roll back the transaction.

    Why it's wrong here

    While ending a session will eventually clean up resources, it does not explicitly abort the transaction. The transaction may remain open until the session times out, and the update might still be applied if the transaction is later committed by another operation. It is not a reliable way to roll back; explicit abortTransaction() is required for deterministic behavior.

About these practice questions

This C100DEV question is part of Courseiva's 259-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official MongoDB exam blueprint

This C100DEV practice question is part of Courseiva's free MongoDB certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the C100DEV exam.