Firestore:ID在集合中或全局是唯一的吗? [英] Firestore: Are ids unique in the collection or globally?
问题描述
对于我的数据库结构,我需要知道Firebase Cloud Firestore中自动生成的标识符在集合中还是在整个数据库中是唯一的.
For the structure of my database I need to know if the automatically generated identifiers in Firebase Cloud Firestore are unique in the collection or in the whole database.
该怎么做?
推荐答案
通过调用 add()
并不与您调用add()
的集合相关.取而代之的是,它们是随机标识符,从统计学上保证是唯一的.对于Firestore(和Firebase实时数据库),这些密钥是在客户端生成的.
The keys generated by calling add()
in Firestore are not tied to the collection on which you call add()
. Instead they are random identifiers that are statistically guaranteed to be unique. In the case of Firestore (and Firebase Realtime Database) these keys are generated client-side.
如果您有兴趣,请查看Firestore JavaScript SDK如何实现该逻辑:
If you're interested, have a look at how the Firestore JavaScript SDK implements the logic:
AutoId.newId()
generates a client-side ID
其本身的逻辑类似于Firebase Realtime数据库如何生成其推送ID.主要区别似乎在于Firestore的自动生成的密钥不是基于本地时间戳,因此无法有效地使用它们来对集合中的文档进行排序.
In itself the logic is similar to how the Firebase Realtime Database generates its push IDs. The main difference seems to be that Firestore's auto-generated keys are not based on the local timestamp, so they cannot be meaningfully used to order the documents in the collection.
这篇关于Firestore:ID在集合中或全局是唯一的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!