Firebase中的多对多关系 [英] Many to Many relationship in Firebase

查看:193
本文介绍了Firebase中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firebase数据库。我有公司和承包商。承包商可以为多个公司工作,而公司可以有多个承包商。这是一个直截了当的多对多的关系。我希望能够回答有关公司和承包商的问题:


  1. 给定一个公司,谁是现在的承包商。

  2. 给承包商什么公司是他们的工作。

在firebase中构建数据有哪些选择?

解决方案

自我确实是建模的一种方式。这可能是您在关系数据库中建模的最直接的等价物:
$ b


  • 承包商

  • companies

  • companyAndContractorsAssignment(多对多连接器表)



将使用4个顶级节点:$ b​​
$ b $ ul

  • 承包商

  • 公司

  • companycontractors

  • contractorCompanies



  • 最后两个节点看起来像:

      companyContractors 
    companyKey1
    contractorKey1:true
    contractorKey3:true
    companyKey2
    contractorKey2:true
    contractorCompanies
    contractorKey1
    companyKey1:true
    contractorKey2
    companyKey2:true
    contractorKey3
    companyKey1:true

    这种双向结构允许您查找承包商一个公司和承包商公司,而这些都不需要被查询。这肯定会更快,特别是当您添加承包商和公司时。



    这是否对您的应用程序是必需的取决于您需要的用例,数据大小你期待和更多。



    推荐阅读 NoSQL数据建模和查看针对SQL开发人员的Firebase 。这个问题也出现在 #AskFirebase youtube系列剧集中。



    更新(2017016)



    有人张贴了后续问题,链接在这里关于检索实际项目从承包商和公司节点。您需要一次检索这些文件,因为Firebase没有等同于 SELECT * FROM table WHERE id IN(1,2,3)。但是这个操作并没有你想像的那么慢,因为这些请求是通过一个连接流水线化的。阅读更多关于这里:通过使用查询而不是重复观察单个事件来加速获取社交网络应用的帖子

    I have a Firebase database. I have Companies and Contractors. A Contractor can work for more than one Company and a Company can have multiple Contractors. This is a straightforward many to many relationship. I want to be able to answer the questions about Companies and Contractors:

    1. Given a Company, who are the current Contractors.
    2. Given a Contractor what Companies are they working for.

    What are the alternatives for structuring the data within firebase?

    解决方案

    The self-answer is indeed one way of modeling this. It's probably the most direct equivalent of how you'd model this in a relational database:

    • contractors
    • companies
    • companyAndContractorsAssignment (the many-to-many connector table)

    An alternative would be to use 4 top-level nodes:

    • contractors
    • companies
    • companyContractors
    • contractorCompanies

    The last two nodes would look like:

    companyContractors
        companyKey1
            contractorKey1: true
            contractorKey3: true
        companyKey2
            contractorKey2: true
    contractorCompanies
        contractorKey1
            companyKey1: true
        contractorKey2
            companyKey2: true
        contractorKey3
            companyKey1: true
    

    This bidirectional structure allows you to both look up "contractors for a company" and "companies for a contractor", without either of these needing to be a query. This is bound to be faster, especially as you add contractors and companies.

    Whether this is necessary for your app, depends on the use-cases you need, the data sizes you expect and much more.

    Recommended reading NoSQL data modeling and viewing Firebase for SQL developers. This question was also featured in an episode of the #AskFirebase youtube series.

    Update (2017016)

    Somebody posted a follow-up question that links here about retrieving the actual items from the "contractors" and "companies" nodes. You will need to retrieve those one at a time, since Firebase doesn't have an equivalent to SELECT * FROM table WHERE id IN (1,2,3). But this operation is not as slow as you may think, because the requests are pipelined over a single connection. Read more about that here: Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly.

    这篇关于Firebase中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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