Short answer
Decide what should happen when the document exists. To replace it, use set(). To change some fields and keep the rest, use set() with merge: true. For a new document every time, use add() or doc() with no ID. If the document must not be overwritten, keep create() and treat code 6 as “already done”.
Rather not fix it yourself? App Fix Ticket starts with a fixed-price diagnosis, then a fixed price for the fix before any fix work.
Why the document exists, and the fix
01
create() only writes new documents
“Create a document with the provided object values. This will fail the write if a document exists at its location.” Google Cloud Node.js client, DocumentReference.create()
In the Admin SDK, create() fails when the ID is taken, on purpose. batch.create() and transaction.create() work the same way.
02
You meant to overwrite or merge
“If the document does exist, its contents will be overwritten with the newly provided data” Firebase, Add data
set() creates the document if it is missing and replaces it if it exists. Add merge: true to keep fields you don’t send.
await db.doc(`users/${uid}`).set({ plan: 'pro' }, { merge: true });03
A retry ran after the first write worked
A function that retries after a timeout can run create() twice. The first one wrote the document, so the second fails. Treat code 6 as success when the document is the one you meant to write.
try { await ref.create(data); } catch (err) { if ((err as { code?: number }).code !== 6) throw err; // 6 = ALREADY_EXISTS }04
The ID isn’t as unique as it looks
IDs built from an email, a date or a name can repeat. Use add() or doc() with no ID for a random one, or keep the natural ID and expect the error.
Checked against the vendor’s docs on : Google Cloud: Firestore Node.js DocumentReference; Firebase docs: Add data to Cloud Firestore.
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.See scope and start
“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
Related: Firestore transaction too big · Firestore unsupported field value: undefined · Firestore query requires an index · Firebase “Missing or insufficient permissions”