0

Could a unique ID be a duplicate if I generate an ID from a different path? like:

const id1 = firestore.collection("chat").doc().id;
const id2 = firestore.collection("profile").doc().id;

Is there any chance id1 and id2 could duplicate?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
flutroid
  • 1,166
  • 8
  • 24
  • Nope, it's just a random generated number on your computer. – Mises Feb 18 '23 at 07:55
  • @Mises Random numbers can in fact come up with a duplicate. It's just extremely unlikely to do so with the size of the string it generates, so unlikely that we just don't worry about it. – Doug Stevenson Feb 18 '23 at 14:11
  • @DougStevenson Yeah I know that i just not mention it so people no need to worry about that. – Mises Feb 18 '23 at 14:25

1 Answers1

2

Is there any chance id1 and id2 could duplicate?

The collision of IDs in the case of Firestore is incredibly unlikely and you can/should consider they'll be completely unique. That's what they were designed for. So you don't have to worry about it.

Keep in mind that, the built-in generator for unique IDs which is used in Firestore when you call the CollectionReference#add() method or CollectionReference#document() method without passing any arguments, generates random and highly unpredictable ids, which prevents hitting certain hotspots in the backend infrastructure.

If you're interested in how exactly the document IDs are generated, please check @FrankvanPuffelen's answer from the following post:

Does a unique ID could duplicate if I generate an ID from a different path?

No. Each time you call .doc().id, a new random document ID is generated.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193