Firestore数据库模型,文档或子集合中的属性 [英] Firestore DB model, Attribute in document or subcollection

查看:30
本文介绍了Firestore数据库模型,文档或子集合中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为Firebase/Firestore建模时,我经常碰到这种分裂的道路,也许您可​​以对此有所了解.

I often come across this split roadwhen modeling Firebase/Firestore, perhaps you can shed light on this.

如果我有多位管理员,则每个客户(100名)都可以使用他们的名字.管理员看不到其他管理员客户端(将其视为独立的公司来考虑).哪个会更好?

If I have multiple admins who can each clients (100s) under their name. The admin can't see the other admin clients (consider them like separate companies). What would be better?

1)添加客户"根集合,并在其下添加一个ID为管理电子邮件的文档,并在该文档下添加一个带有客户"文档的子集合:

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:

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

2)或添加带有其下的客户文档的客户根集合,但每个文档中的一个属性将是管理员电子邮件:

2) OR Add clients root collection with clients documents under it but an attribute in every document would be the admin email:

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.

但是,如果您想使用第二个选项实现相同的目的,则只有在您使用基于单个属性( uid 属性)的查询时,这才可能实现这个:

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:

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);

您需要知道您无法实现这一目标.这是不可能的,因为在这种情况下,您需要创建一个索引并且您不能为每个 uid 手动创建索引.

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.

这篇关于Firestore数据库模型,文档或子集合中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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