Firestore在集合中生成的密钥与自定义密钥? [英] Firestore generated key versus custom key in a collection?

查看:76
本文介绍了Firestore在集合中生成的密钥与自定义密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android应用中使用Cloud Firestore数据库,并且集合中有不同的文档,例如:用户uid,餐厅的按键和菜谱编号.

I am using Cloud Firestore database in my Android app and I have different documents within collections like: uid for users, pushed keys for restaurants and numbers for my recipes.

我的数据库:

users
  uid1
  uid2
  ...
resturants
  pushedId1
  pushedId2
  ...
recipes
  0001
  0002
  ...

对于我理解的用户,我可以使用uid,但最好为我的餐厅使用Firestore推送的ID?这是惯例还是为什么要使用它?

For the users I understand to use the uid's but is better to use Firestore pushed ids for my restaurants? Is this a convention or why to use it?

我还尝试使用 UUID类生成唯一键,但对于我来说更容易在食谱中只使用数字.这是一个不好的方法吗?

I also tried to generate unique keys using UUID Class but is more easy for me to use only numbers for my recipes. Is this a bad approach?

任何帮助将不胜感激,谢谢!

Any help will be appreciated, thank you!

推荐答案

通过为文档使用可预测的(例如顺序的)ID,可以增加在后端基础架构中遇到热点的机会.这会降低 write 操作的可伸缩性.

By using predictable (e.g. sequential) IDs for documents, you increase the chance you'll hit hotspots in the backend infrastructure. This decreases the scalability of the write operations.

Cloud Firestore具有内置的唯一ID生成器,当您调用

Cloud Firestore has a built-in generator for unique IDs, that is used when you call CollectionReference.add(...) or CollectionReference.document() (without parameters). The ID that it generates is random and highly unpredictable, which prevents hitting certain hotspots in the backend infrastructure.

使用用户文档的UID可以很好地替代Firestore的内置生成器,因为UID已经具有很高的熵:您无法基于了解当前用户来预测下一个用户的UID.在这种情况下,使用UID(或实体的自然键)是一种更好的方法,因为您可以直接查找文档,而不必查询.

Using UIDs for the documents of users is a fine substitute for Firestore's built-in generator, since the UIDs already have a high level of entropy: you can't predict the UID of the next user based on knowing the current user. In such a case, using the UID (or otherwise the natural key of the entity) is a better approach, since you can perform direct lookups of the documents instead of having to query.

在firebase-talk邮件列表中查看此讨论在Firestore上工作的一些工程师在其中进行了更详细的说明.

See this discussion on the firebase-talk mailing list where some of the engineers working on Firestore explain in more detail.

这篇关于Firestore在集合中生成的密钥与自定义密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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