Is it possible to receive duplicate IDs with Firebase .doc()?

As specified in the Firebase documentation, .doc() creates a unique ID.

I create new documents for Firestore the following way (note that session.docId should be equal to the actual document ID and I am using AngularFire syntax):

const id = db.createId();
const session = { docId: id, name: "test" };

Does .doc() guarantee that the above workflow will always work (meaning that id will always be unique)?
Let’s assume I have an array full of sessions (e.g. 10000 items) – I iterate over this array and create a session document for each item the way I specified it above. Considering the asynchronous nature of Javascript, I am very sceptical if this might produce a duplicate ID at some point (although very unlikely due to the length etc. of the docId). Or does .doc() "reserve" the ID for a limited amount of time?

I am aware that I could also create a new document via .add() and update the docId afterwards via .set(), but this method would need 2 write operations and would also be way slower.

21 thoughts on “Is it possible to receive duplicate IDs with Firebase .doc()?”

Leave a Comment