从父节点读取特定叶/子节点的 Firebase 规则
Firebase Rules to read Specific Leaf / Child Node from Parent Nodes
我的 firebase 数据库是这样的
"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 检索数据。休息api url 看起来像 https://domain.firebaseio.com/students.json?orderby="Marks/Total"&startAt=400
我已经通过 firebase 规则为学生总数编制了索引。我得到结果以及额外的数据,例如姓名、class、卷号
我希望输出为
"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 执行此操作。
是否有任何规则可以定义要读取的节点,例如
{
"users":{
"students":{
".read" : ["$firebaseKey/Marks"],
".write" : true,
}
}
这样我就可以使用 Rest api 从父节点检索所需的值。
如果有任何其他建议,那就太好了。
提前致谢
Firebase 数据库始终 returns 完整节点。不可能获得与您的查询匹配的每个节点的子集。要么返回整个节点,要么不返回。
通常这种类型的请求表明您合并了多种类型的数据,您应该将它们分开。在您的情况下,看起来您应该有两个顶级集合:students
和 studentMarks
。在 students
下,您保留每个学生的属性,以他们的学生 ID 为关键字。在 studentMarks
下,您保留每个学生的分数,同样由他们的学生 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",
}
}
由于您在 students
和 studentMarks
之间使用相同的密钥,您可以轻松地为用户准备两组数据。但现在您也可以只读取每个用户的属性,或者只读取一组用户的标记。
我的 firebase 数据库是这样的
"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 检索数据。休息api url 看起来像 https://domain.firebaseio.com/students.json?orderby="Marks/Total"&startAt=400
我已经通过 firebase 规则为学生总数编制了索引。我得到结果以及额外的数据,例如姓名、class、卷号
我希望输出为
"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 执行此操作。
是否有任何规则可以定义要读取的节点,例如
{
"users":{
"students":{
".read" : ["$firebaseKey/Marks"],
".write" : true,
}
}
这样我就可以使用 Rest api 从父节点检索所需的值。
如果有任何其他建议,那就太好了。
提前致谢
Firebase 数据库始终 returns 完整节点。不可能获得与您的查询匹配的每个节点的子集。要么返回整个节点,要么不返回。
通常这种类型的请求表明您合并了多种类型的数据,您应该将它们分开。在您的情况下,看起来您应该有两个顶级集合:students
和 studentMarks
。在 students
下,您保留每个学生的属性,以他们的学生 ID 为关键字。在 studentMarks
下,您保留每个学生的分数,同样由他们的学生 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",
}
}
由于您在 students
和 studentMarks
之间使用相同的密钥,您可以轻松地为用户准备两组数据。但现在您也可以只读取每个用户的属性,或者只读取一组用户的标记。