Firebase Firestore-关系数据设计方法 [英] Firebase Firestore - relational data design approaches
问题描述
我对Firebase完全陌生,我正在努力使有关1-1和1-many的关系"数据的最佳数据库模型设计成为现实.
I'm totally new to Firebase, and I'm trying to get my head round the best db model design for 'relational' data, both 1-1 and 1-many.
我们正在使用 Firestore 数据库(不是实时数据库).
We are using the Firestore db (not the realtime db).
假设我们有Projects
,其中可以包含许多Users
,而User
可以位于多个Projects
Say we have Projects
which can contain many Users
, and a User
can be in multiple Projects
用户界面需要在Project
中显示Users
的列表,该列表显示email
,firstname
,lastname
和department
之类的内容.
The UI needs to show a list of Users
in a Project
which shows things like email
, firstname
, lastname
and department
.
存储关系的最佳方法是什么?
What is the best way to store the relationship?
-
Project
文档中的User
id数组? -
Project
文档中的ID映射?
- An array of
User
ids in theProject
document? - A map of Ids in the
Project
document?
我已经读过以上建议的方法,但是对于实时数据库而言? Firestore 支持Sub Collection,听起来更合适...
Ive read the above approaches were recommended, but was that for realtime database? Firestore supports Sub Collections, which sound more appropriate...
- 项目文档中
Users
的子集合吗? - 将单独的集合
Project
id映射到User
id? -
Reference
数据类型?我已经在这里阅读 https://firebase.google.com/docs/关于Reference
数据类型的firestore/manage-data/data-types ,听起来像我想要的,但是我找不到更多内容!
- A sub collection of
Users
in the Project document? - A separate collection mapping
Project
id toUser
id? - A
Reference
data type? I've read here https://firebase.google.com/docs/firestore/manage-data/data-types aboutReference
data type, which sounds like what I want, but I cant find any more on it!
如果它只是一个ID的映射或数组,那么您将如何检索有关用户的其余数据?这是否必须坐在应用程序UI中?
If its just a map or array of Ids, how would you then retrieve the remaining data about the user? Would this have to sit in the application UI?
如果它是用户文档的子集合,是否有任何方法可以维护数据完整性?如果用户更改了名称,那么UI/cloudFunction是否必须更新Sub集合中该用户名称的每个条目?
If its a sub collection of Users documents, is there any way to maintain data integrity? If a user changed their name, would the UI / a cloudFunction then have to update every entry of that users name in the Sub collections?
任何帮助/指针都表示赞赏...
any help / pointers appreciated...
推荐答案
在Firestore中建模多对多关系的方法与在Firebase的实时数据库"中的方法几乎相同,我在这里已经回答: Firebase中的多对多关系.确实唯一的区别是,您可以将查找列表存储在每个项目/用户的子集合中.
The approach for modeling many-to-many relationships in Firestore is pretty much the same as it was in Firebase's Realtime Database, which I've answered here: Many to Many relationship in Firebase. The only difference is indeed that you can store the lookup list in a sub-collection of each project/user.
查找链接项也与以前相同,确实需要从客户端分别加载它们.这样的客户端连接速度并没有您最初预期的那么慢,因此请在假定连接速度不够快之前对其进行测试.
Looking up the linked item is also the same as before, it indeed requires loading them individually from the client. Such a client-side join is not nearly as slow as you may initially expect, so test it before assuming it can't possibly be fast enough.
通过执行分批写入或使用交易.这些要么完全成功,要么完全失败.
Ensuring data integrity can be accomplished by performing batched writes or using transactions. These either completely succeed or completely fail.
这篇关于Firebase Firestore-关系数据设计方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!