Firestore 自动生成的 ID 是否依赖于路径?

huangapple go评论64阅读模式
英文:

Does firestore auto generated id depend on path?

问题

有没有可能 id1id2 重复?

英文:

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?

答案1

得分: 2

Is there any chance id1 and id2 could duplicate?

在Firestore的情况下,ID的碰撞是极不可能的,你可以/应该认为它们将是完全唯一的。这就是它们被设计出来的原因。所以你不必担心。

请记住,Firestore中用于生成唯一ID的内置生成器,当你调用CollectionReference#add()方法或CollectionReference#document()方法而不传递任何参数时,会生成随机且高度不可预测的ID,这可以防止在后端基础设施中命中某些热点。

如果你对文档ID的生成方式有兴趣,请查看以下帖子中@FrankvanPuffelen的回答:

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

不会。每次你调用.doc().id,都会生成一个新的随机文档ID。

英文:

> 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.

huangapple
  • 本文由 发表于 2023年2月18日 15:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定