Prags
Prags

Reputation: 811

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

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

Upvotes: 0

Views: 363

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

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.

Upvotes: 1

Related Questions