当我们有多对多关系时,如何在 firebase 中获取数据 [英] how to get data within firebase, when we have many to many relationship

查看:24
本文介绍了当我们有多对多关系时,如何在 firebase 中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了这个问题Firebase 中的多对多关系
这里描述了如何在 firebase(nosql 数据库)中创建或构建多对多关系,而且很简单,

I read this question Many to Many relationship in Firebase,
and here describes how to create or structure many to many relationship within firebase(nosql database), and it is pretty easy,

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

但是当我想获得所有承包商的特定公司时,我可以只使用一个请求吗?因为在这种情况下,我只得到 id 列表,然后使用 js 循环或 forEach 执行多个请求.

but when i want to get all contractors specific company, can i do it using only one request? because in this case i get only list of id, and after there, using js loop, or forEach, i do multiple request.

通常在其他 API 上,我只使用

usually on others API's, i only use

URL/contractor/:id/company or 
URL/company/:id/contractors 

如何在firebase上做到这一点?
有一个使用 angular2、angularfire2 的例子会很酷
谢谢

how to do it on firebase?
it would be cool to have an example of using angular2, angularfire2
thanks

推荐答案

使用 NoSQL,您必须首先从视图的角度进行思考,然后让视图决定您的架构.你绝对可以用一个查询来做到这一点,但忘记规范化,这个概念只适用于关系数据库.

With NoSQL, you have to think in terms of views first, then let the views dictate your schema. You can definitely do this with one query, but forget about normalization, that concept only applies to relational databases.

假设您有一个视图,您想按公司搜索并列出所有承包商及其信息,或按承包商搜索并列出所有公司及其信息:

Let's say you have a view where you want to search by company and list all the contractors, with their info, or search by contractor and list all the companies with their info:

架构:companyContractorKey、contractorCompanykey、c​​ontractorName、contractorSkill、companyIndustry 等...

Schema: companyContractorKey, contractorCompanykey, contractorName, contractorSkill, companyIndustry, etc...

其中 companyContractorKey 是包含公司名称和承包商名称串联的字段,例如:Acme/Ellis Electric".然后,您可以进行从Acme/A"到Acme/z"的范围搜索,并获取 Acme 的所有承包商.

where companyContractorKey is a field containing a concatenation of the company name and contractor name, for example: 'Acme/Ellis Electric'. You can then do a range search from 'Acme/A' to 'Acme/z' and get all the contractors for Acme.

同样,contractorCompanyKey 是一个包含承包商名称和公司串联的字段,例如Ellis Electric/Acme".然后,您可以进行从Ellis Electric/A"到Ellis Electric/z"的范围搜索,以获取 Ellis Electric 的所有公司.

Similarly, contractorCompanyKey is a field containing a concatenation of the contractor name and company, for example 'Ellis Electric/Acme'. You can then do a range search from 'Ellis Electric/A' to 'Ellis Electric/z' to get all the companies for Ellis Electric.

缺点是一个公司的信息存储在多条记录中(使用companyContractorKey很容易找到),而一个承包商的信息也存储在多条记录中(使用contractorCompanyKey找到),所以更新和删除都会涉及多条记录,但是查询会超级快,只要你索引两个关键字段.Firebase 支持通过一个请求更新多条记录,因此这应该不会出现问题.

The drawback is that the information for a company is stored in multiple records (easily found using the companyContractorKey), and the information for a contractor is also stored in multiple records (found using the contractorCompanyKey), so updates and deletions will involve multiple records, but querying will be super fast, as long as you indexOn the two key fields. Firebase supports updating multiple records with one request, so this should not present a problem.

此外,您还需要避免在该架构节点中放入有关公司或承包商的所有信息,只放入您的视图所需的信息,并将所有不在列表"视图中的详细信息放在单独的架构节点中,一份专供公司使用,另一份专供承包商使用.

Also you will want to avoid putting in all the information about a company or contractor in that schema node, only what is necessary for your views, and have all the details that are not in the "listing" view in separate schema nodes, one dedicated to companies and one to contractors.

这篇关于当我们有多对多关系时,如何在 firebase 中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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