简体   繁体   English

Firestore数据库模型,文档或子集合中的属性

[英]Firestore DB model, Attribute in document or subcollection

I often come across this split roadwhen modeling Firebase/Firestore, perhaps you can shed light on this. 在为Firebase / Firestore建模时,我经常碰到这种分裂的道路,也许您可​​以对此有所了解。

If I have multiple admins who can each clients (100s) under their name. 如果我有多位管理员,则每个客户(100位)都可以使用他们的名字。 The admin can't see the other admin clients (consider them like separate companies). 管理员看不到其他管理员客户(将其视为独立的公司来考虑)。 What would be better? 有什么会更好?

1) Add Clients root collection and under it add a document with ID being the admin email and under that a subcollection with clients documents: 1)添加“客户”根集合,并在其下添加一个ID为管理电子邮件的文档,并在该子集合下添加“客户”文件:

Firestore-root
   |
   --- Clients(collection)
         |
         --- Admin ID/Email(Collection)
              |
              ----------Clients Info (documents)

2) OR Add clients root collection with clients documents under it but an attribute in every document would be the admin email: 2)或添加带有客户文档的客户根集合,但每个文档中的一个属性是管理员电子邮件:

Firestore-root
   |
   --- Clients(collection)
         |
         --- Clients Info (documents)
             |
              --- AdminId: (email)

I find the first one is easier to query and most importantly more readable if you want to view the data in the console during testing/or even production. 我发现第一个更易于查询,最重要的是,如果要在测试/甚至生产期间在控制台中查看数据,则第一个更易于阅读。 But I find the second one is less number of levels. 但是我发现第二个是较少的级别。

What is the right way to approach this? 解决这个问题的正确方法是什么? Thank you 谢谢

There is no right way for a database structure. 数据库结构没有正确的方法。 The right solution for your database, is the solution that fits your needs and makes your job easier. 适合您数据库的正确解决方案是适合您的需求并使您的工作更轻松的解决方案。 My opinion regarding your database schema is related actually on what you really want to achieve. 我对数据库架构的看法实际上与您真正想要实现的目标有关。

If you want to query your database to get only the clients that correspond to a particular admin, then you can go ahead with the first option. 如果要查询数据库以仅获取与特定管理员对应的客户端,则可以继续使用第一个选项。 If you want at some point to get all the clients from your database, then the second option will help you achieve this. 如果您希望在某个时候从数据库中获取所有客户端,那么第二个选项将帮助您实现这一目标。 So it might be an option to use both options. 因此,使用这两个选项可能是一个选择。

But, if you want to achieve the same thing using the second option, this can be possible only if you'll use a query that is based on a single property (the uid property) like this: 但是,如果您想使用第二个选项来实现相同的目的,则只有在您使用基于单个属性( uid属性)的查询时,才有可能这样做:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference clientsRef = rootRef.collection("Clients");
Query query = clientsRef.whereEqualTo("uid", uid);

But if you want instend to achieve the same thing as in the first option and use a query that looks like this: 但是,如果您希望实现与第一个选项相同的功能,并使用如下查询:

Query query = clientsRef.whereEqualTo("uid", uid).orderBy("aProperty", Query.Direction.ASCENDING);

You need to know that you cannot achieve this. 您需要知道您无法实现这一目标。 This is not possible because in this case you need to create an index and you cannot create manually an index for each uid . 这是不可能的,因为在这种情况下,您需要创建一个索引,并且不能为每个uid手动创建索引。

One more thing, I recommend you to use the uid instead of an email address as the document key. 还有一件事,我建议您使用uid而不是email address作为文档密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM