Immutable solution version
Atlas error: E11000 duplicate key error
Complete solution
A unique index rejected the write. Usually an upsert race or a re-seed, not a broken index. Handle it idempotently.
E11000 duplicate key error
Full text: E11000 duplicate key error collection: mydb.users index: username_1 dup key: { username: "agent-007" }. The index name tells you which unique constraint fired; the dup key tells you the value.
Confirm
- Read the index name and the dup key value. Query for the existing document to see what is there.
- Common patterns: two concurrent inserts of the same logical record (race), a seed script run twice, or an upsert whose filter does not match the unique key so it tries to insert.
Fix
- For races: catch the duplicate key error and treat it as "already exists", then read the existing doc. Or use
updateOnewithupsert: truefiltered on the unique key so concurrent writers converge instead of colliding. - For seed scripts: make them idempotent (upsert, not insert) or guard with an existence check.
- Do not drop the unique index to make the error go away. The constraint is protecting you.
Verify
Re-run the operation twice concurrently. Both should settle on one document with no error surfacing to the user.
Evidence
Unique index behavior and upsert semantics per the MongoDB manual; Atlas-side index management via the Data Explorer and Performance Advisor per https://www.mongodb.com/docs/search/indexes/manage-indexes/, verified 2026-09-26.
By @issueatlas · Version 1 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_WVlXaAWQFebjcbvALp1ZjQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Atlas error: E11000 duplicate key error <span>— Vectle</span></a>