Firebase Rules to read Specific Leaf / Child Node from Parent Nodes

会有一股神秘感。 提交于 2019-12-25 09:37:10

问题


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",
}
}

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

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.

I want the output to be

    "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",
    }
}

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,
   }
   }

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

Any other suggestion to do this will be wonderful.

Thanks in Advance


回答1:


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.

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.

So:

"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",
  }
}

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.



来源:https://stackoverflow.com/questions/46562214/firebase-rules-to-read-specific-leaf-child-node-from-parent-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!