Firebase规则从父节点读取特定的叶子/子节点 [英] Firebase Rules to read Specific Leaf / Child Node from Parent Nodes

查看:91
本文介绍了Firebase规则从父节点读取特定的叶子/子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebase数据库如下所示

My firebase database looks like this

"students" : {
  "firebase_key_1" : {
    "Name" : "blah blah",
    "Address" : "blah blah",
    "Roll No" : "blah blah",
    "Marks" : {
        "Sub1" : "blah",
        "Sub2" : "blah",
        "Sub3" : "blah",
        "Total" : "Total",
    },
    "class" : "blah blah",
    "Transportation" : "blah blah",
    "Department" : "blah blah",
},
  "firebase_key_2" : {
    "Name" : "blah blah",
    "Address" : "blah blah",
    "Roll No" : "blah blah",
    "Marks" : {
        "Sub1" : "blah",
        "Sub2" : "blah",
        "Sub3" : "blah",
        "Total" : "Total",
    },
    "class" : "blah blah",
    "Transportation" : "blah blah",
    "Department" : "blah blah",
},
  "firebase_key_3" : {
    "Name" : "blah blah",
    "Address" : "blah blah",
    "Roll No" : "blah blah",
    "Marks" : {
        "Sub1" : "blah",
        "Sub2" : "blah",
        "Sub3" : "blah",
        "Total" : "Total",
    },
    "class" : "blah blah",
    "Transportation" : "blah blah",
    "Department" : "blah blah",
}
}

我正在使用rest api从firebase检索数据. Restapi网址类似于 > https://domain.firebaseio.com/students.json? orderby = 标记/总数"& startAt = 400

I am using rest api to retrieve the data from firebase. Restapi url looks like https://domain.firebaseio.com/students.json?orderby="Marks/Total"&startAt=400

我已经通过Firebase规则对学生总数进行了索引.我得到了Result以及其他数据,例如名称,类,卷号.

I have already indexed the Total in students via firebase rules. I am getting Result along with extra data such as name, class, roll no.

我希望输出为

    "firebase_key_1" : {
    "Marks" : {
        "Sub1" : "blah",
        "Sub2" : "blah",
        "Sub3" : "blah",
        "Total" : "Total",
    }
},
"firebase_key_2" : {
    "Marks" : {
        "Sub1" : "blah",
        "Sub2" : "blah",
        "Sub3" : "blah",
        "Total" : "Total",
    }
},
"firebase_key_3" : {
    "Marks" : {
        "Sub1" : "blah",
        "Sub2" : "blah",
        "Sub3" : "blah",
        "Total" : "Total",
    }
}

无论如何,可以通过RestAPi或Rules来做到这一点.

Is there anyway to do this via RestAPi or Rules.

我们是否有任何规则可以定义要读取的节点

Is there any rules that we can define what nodes to be read for example

{
 "users":{
   "students":{
      ".read" : ["$firebaseKey/Marks"],
      ".write" : true,
   }
   }

这样我就可以使用Rest api从父节点检索所需的值.

So that i can use Rest api to retrieve the required values from parent node.

执行此操作的任何其他建议都会很棒.

Any other suggestion to do this will be wonderful.

预先感谢

推荐答案

Firebase数据库始终返回完整的节点.无法获得与查询匹配的每个节点的子集.要么返回整个节点,要么不返回.

The Firebase Database always returns complete nodes. It's not possible to get a subset of each node that matches your query. Either the entire node is returned, or it is not returned.

通常,这种类型的请求表明您已合并了应分开的多种类型的数据.在您的情况下,您似乎应该有两个顶级集合:studentsstudentMarks.在students下,保留每个学生的属性,并按其学生ID进行键控.在studentMarks下,您保留每个学生的分数,并再次用他们的学生ID进行输入.

Typically this type of request indicates that you've merged multiple types of data that you should separate. In your case, it looks like you should have two top-level collections: students and studentMarks. Under students you keep the properties for each student, keyed by their student ID. Under studentMarks you keep the marks for each student, again keyed by their student ID.

所以:

"students" : {
  "firebase_key_1" : {
    "Name" : "blah blah",
    "Address" : "blah blah",
    "Roll No" : "blah blah",
    "class" : "blah blah",
    "Transportation" : "blah blah",
    "Department" : "blah blah",
  },
  "firebase_key_2" : {
    "Name" : "blah blah",
    "Address" : "blah blah",
    "Roll No" : "blah blah",
    "class" : "blah blah",
    "Transportation" : "blah blah",
    "Department" : "blah blah",
  },
  "firebase_key_3" : {
    "Name" : "blah blah",
    "Address" : "blah blah",
    "Roll No" : "blah blah",
    "class" : "blah blah",
    "Transportation" : "blah blah",
    "Department" : "blah blah",
  }
},
"studentMarks": 
  "firebase_key_1" : {
    "Sub1" : "blah",
    "Sub2" : "blah",
    "Sub3" : "blah",
    "Total" : "Total",
  },
  "firebase_key_2" : {
    "Sub1" : "blah",
    "Sub2" : "blah",
    "Sub3" : "blah",
    "Total" : "Total",
  },
  "firebase_key_3" : {
    "Sub1" : "blah",
    "Sub2" : "blah",
    "Sub3" : "blah",
    "Total" : "Total",
  }
}

由于在studentsstudentMarks之间使用了相同的键,因此可以轻松地为用户准备两组数据.但是现在您也可以只读取每个用户的属性,或仅读取一组用户的标记.

Since you're using the same key between students and studentMarks you can easily ready both sets of data for a user. But now you can also just read just the properties for each user, or just the marks for a set of users.

这篇关于Firebase规则从父节点读取特定的叶子/子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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