Firestore:集合中的ID是唯一的还是全局的?

时间:2023-01-30 01:27:02

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.

对于我的数据库结构,我需要知道Firebase Cloud Firestore中自动生成的标识符在集合中或整个数据库中是唯一的。

Do you know? Or do you know how I can find an answer to this question?

你知道吗?或者你知道我怎么能找到这个问题的答案吗?

1 个解决方案

#1


21  

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中调用add()生成的密钥与您调用add()的集合无关。相反,它们是随机标识符,在统计上保证是唯一的。对于Firestore(和Firebase实时数据库),这些密钥是在客户端生成的。

If you're interested, have a look at how the Firestore JavaScript SDK implements the logic:

如果您有兴趣,请查看Firestore JavaScript SDK如何实现逻辑:

  1. add calls doc()

    添加调用doc()

  2. doc calls AutoId.newId()

    doc调用AutoId.newId()

  3. AutoId.newId() generates a client-side ID

    AutoId.newId()生成客户端ID

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.

其逻辑本身与Firebase实时数据库生成推送ID的方式类似。主要区别似乎是Firestore的自动生成的密钥不是基于本地时间戳,因此它们无法有意义地用于订购集合中的文档。

#1


21  

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中调用add()生成的密钥与您调用add()的集合无关。相反,它们是随机标识符,在统计上保证是唯一的。对于Firestore(和Firebase实时数据库),这些密钥是在客户端生成的。

If you're interested, have a look at how the Firestore JavaScript SDK implements the logic:

如果您有兴趣,请查看Firestore JavaScript SDK如何实现逻辑:

  1. add calls doc()

    添加调用doc()

  2. doc calls AutoId.newId()

    doc调用AutoId.newId()

  3. AutoId.newId() generates a client-side ID

    AutoId.newId()生成客户端ID

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.

其逻辑本身与Firebase实时数据库生成推送ID的方式类似。主要区别似乎是Firestore的自动生成的密钥不是基于本地时间戳,因此它们无法有意义地用于订购集合中的文档。