Firestore:客户和发票,如何建模 [英] Firestore: Clients and invoices, how to model it

查看:38
本文介绍了Firestore:客户和发票,如何建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下架构,但不确定如何在Firestore中对其进行建模.

I have the following schema and I am not sure how to model it in Firestore.

我将有客户和发票.客户有"发票.我需要能够执行以下两个查询:

I will be having clients and invoices. Clients "has" invoices. I need to be able to do these 2 queries:

  • 显示客户的发票
  • 更新系统中的所有发票(将布尔属性从true更改为false).

对此建模的正确方法是什么?通过收集具有发票子集的客户来满足第一个查询.但是第二张帐单是否有所有帐单的集合,这是令人满意的吗?

What would be the right way to model this? The first query is satisfied by having a collection of clients with subcollection of their invoices. But the second one is satisfied by having a collection of all invoices?

感谢任何经验丰富的帮助

Any experienced help is appreciated

谢谢

推荐答案

我还有另一项建议,其中涉及您创建两个这样的顶级集合:

I have another recommendation which involves you to create two top level collections like this:

Firestore-root
   |
   --- users (collection)
   |     |
   |     --- userId (documents)
   |          |
   |          --- //user details
   |
   --- invoices (collection)
         |
         --- invoiceId (documents)
              |
              --- yourBooleanProperty: true
              |
              --- userId: true

如您所见,最简单的方法是拥有一个名为 invoices 的单一集合,该集合可以将数据库中所有发票作为文档保存.由于单个发票只能属于一个用户,因此可以将 userId 作为属性.要获取与特定用户相对应的所有发票,建议您使用以下查询:

As you can see, the simplest way to achieve this, is to have a single collection named invoices that can hold as documents all the invoices in your database. Because a single invoice can belong only to a single user, you can have the userId as a property. To get all the invoices that correspond to a specific user, I recommend you to use the following query:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("invoices").whereEqualTo(userId, true);

如果您想一次将所有发票的布尔属性从 true 更改为 false ,只需使用以下查询:

And if you want to change the boolean property of all invoices from true to false at once, simply use the following query:

Query query = rootRef.collection("invoices").whereEqualTo(yourBooleanProperty, true);

这篇关于Firestore:客户和发票,如何建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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