Firestore:ID 在集合中还是全局唯一? [英] Firestore: Are ids unique in the collection or globally?

查看:27
本文介绍了Firestore:ID 在集合中还是全局唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的数据库结构,我需要知道 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() 在 Firestore 中与您调用 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:

  1. add 调用 doc()

doc 调用 AutoId.newId()

AutoId.newId() 生成客户端 ID

AutoId.newId() generates a client-side ID

本身的逻辑类似于 Firebase 实时数据库生成其推送 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆