【问题标题】:Firebase Rules to read Specific Leaf / Child Node from Parent Nodes从父节点读取特定叶/子节点的 Firebase 规则
【发布时间】:2017-10-04 10:08:06
【问题描述】:

我的 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 检索数据。 Restapi 网址看起来像 https://domain.firebaseio.com/students.json?orderby="Marks/Total"&startAt=400

我已经通过 firebase 规则为学生总数编制了索引。我得到了 Result 以及额外的数据,例如姓名、班级、卷号。

我希望输出是

    "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 或规则来做到这一点。

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

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

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

任何其他这样做的建议都会很棒。

提前致谢

【问题讨论】:

  • 我不明白你想在这里完成什么。您能否编辑您的问题以显示:1)您想要执行的 REST 调用,2)该位置的 JSON(作为文本),3)您希望从调用中返回的结果?
  • 我已经编辑了上面的问题。
  • 这不是规则的有效语法:".read" : ["$firebaseKey/Marks"]

标签: firebase firebase-realtime-database firebase-security


【解决方案1】:

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

通常这种类型的请求表明您已经合并了应该分开的多种类型的数据。在您的情况下,您应该有两个顶级集合:studentsstudentMarks。在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",
  }
}

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

【讨论】:

  • 非常感谢,我会改变结构的。
  • 我认为会有这样的语法“.read”:[“$firebaseKey/Marks”]。类似于 indexOn 这就是为什么我认为我可以坚持这种结构。无论如何,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多