Firebase规则从父节点读取特定叶子节点

时间:2017-10-04 10:08:07

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

我的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规则为学生索引了Total。我得到了结果以及额外的数据,如名字,类,掷号。

我希望输出为

    "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 个答案:

答案 0 :(得分: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之间使用相同的密钥,因此您可以轻松地为用户准备好这两组数据。但现在您也可以只读取每个用户的属性,或只读取一组用户的标记。