**TL;DR:** Two transactions wrote the same document and one lost. This is normal concurrency control, not corruption. Confirm It happens under concurrent writes to overlapping documents, and spikes with long transactions that hold their snapshot while other writers proceed. Fix Retry the entire transaction from the beginning, not just the failed statement.

## The error

```text
TransientTransactionError
```

## The fix

# WriteConflict in transactions

Full text includes `WriteConflict` with an operation that was killed and retried internally. MongoDB uses optimistic concurrency for transactions: the first committer wins, the loser gets WriteConflict.

## Confirm

It happens under concurrent writes to overlapping documents, and spikes with long transactions that hold their snapshot while other writers proceed.

## Fix

Retry the entire transaction from the beginning, not just the failed statement. The standard pattern:

```js
async function runTxn(fn) {
  for (let attempt = 0; attempt < 3; attempt++) {
    const session = client.startSession();
    try {
      let result;
      await session.withTransaction(fn);
      return result;
    } catch (e) {
      const labels = e.errorLabels || [];
      if (labels.includes('TransientTransactionError') && attempt < 2) continue;
      throw e;
    } finally {
      await session.endSession();
    }
  }
}
```

Also shrink the transaction: fewer documents, less work between start and commit, so the conflict window narrows.

## Verify

Run the conflicting workload concurrently. Conflicts still occur occasionally but all attempts eventually commit with no user-visible error.

## When to use this

- You are seeing this exact error message; match the block above, not just part of it.
- The failing call matches the scenario in the title: Atlas error.
- You want the fastest verified fix before digging through logs.

## When not to use this

- Your error text differs from the block above; close cousins often have different causes.
- The stack trace points at a different component than the one in the title.
- You already applied this fix and the error persists; look for a second cause instead of reapplying.

## Compatibility

- Not pinned to a specific version; follows current Atlas behavior.