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

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

问题描述

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


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

但是当我想要得到所有承包商特定的公司,我可以使用只有一个请求吗?因为在这种情况下,我只得到id列表,并在那里,使用js循环,或forEach,我做多个请求。

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

$ p $ URL / contractor /:id / company或
URL / company /:id / contractors

如何在firebase上执行此操作?

使用angular2会很酷, angularfire2


谢谢

$ b

AskFirebase


解决方案使用NoSQL,你必须首先考虑视图,然后让视图决定你的模式。你肯定可以用一个查询来做到这一点,但忘记规范化,这个概念只适用于关系数据库。



假设你有一个视图,你想要由公司搜索并列出所有的承包商,他们的信息,或承包商搜索,并列出所有公司与他们的信息:

架构:
companyContractorKey,contractorCompanykey,contractorName, contractorSkill,
companyIndustry等等。


其中companyContractorKey是一个包含公司名称和承包商名称串联的字段,例如:'Acme / Ellis电动'。然后,您可以执行从Acme / A到Acme / z的范围搜索,并获取Acme的所有承包商。同样,contractorCompanyKey是包含承包商名称和公司的连接,例如Ellis Electric / Acme。然后,您可以从Ellis Electric / A到Ellis Electric / z进行范围搜索,以获得Ellis Electric的所有公司。公司信息存储在多个记录中(使用companyContractorKey很容易找到),而承包商的信息也存储在多个记录中(使用contractorCompanyKey找到),所以更新和删除将涉及多个记录,但是查询将是超级快速,只要你指标上的两个关键领域。 Firebase支持使用一个请求更新多个记录,所以这不应该有问题。



另外,您还需要避免输入关于公司或承包商的所有信息模式节点,只有你的意见是必要的,并有不在单独模式节点的列表视图中的所有细节,一个专用于公司和一个承包商。

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

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.

usually on others API's, i only use

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

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

AskFirebase

解决方案

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:

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

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.

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.

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天全站免登陆