0

Firebase docs covers Filtering by a specified child key. But it assumes that the child value is know. It does not cover a situation where the child is a dynamic value (eg : Student Roll Number)

Specified Child Name Example Data

{
  "Student1": {
    "semister": {
      "english" : 80,
      "maths" : 60
    }
  },
  "Student2": {
    "semister": {
      "english" : 40,
      "maths" : 45
    }
  }
}

Get students who have scored marks higher than 50 in maths

[GET] https://<baseurl>/students.json?orderBy="semister/maths"&startAt=50

Dynamic Child Name Example Data

{
  "Student1": {
    "unit1": {
      "english" : 80,
      "maths" : 60
    },
    "unit2": {
      "english" : 80,
      "maths" : 60
    }
  },
  "Student2": {
    "semister": {
      "english" : 40,
      "maths" : 50
    },
    "unit2": {
      "english" : 80,
      "maths" : 60
    }
  }
}

I get error by using below

Get students who have scored marks higher than 50 in maths in any unit

[GET] https://<baseurl>/students.json?orderBy="$unit/maths"&startAt=50

Any help / suggestion is appreciated

Adarsh Madrecha
  • 6,364
  • 11
  • 69
  • 117
  • 1
    There is no way to query by such a dynamic path. You'll have to restructure your data to allow the query you want. For similar questions, see [here](http://stackoverflow.com/questions/36740657/firebase-equalto-dynamic-nested-child/36745707#36745707), [here](http://stackoverflow.com/questions/34363511/add-a-listener-to-a-child-inside-of-a-firebase-generated-key) and [here](http://stackoverflow.com/questions/29037813/how-to-manage-dynamic-path-in-app-to-firebase-data) (and probably others from [this list](http://stackoverflow.com/search?q=%5Bfirebase%5D+dynamic+path)) – Frank van Puffelen Jul 10 '16 at 14:25

1 Answers1

0

The problem is you don't have a firebase location for all your units. If you want to have more powerful queries, you need to make your data more relational. `

Students: {
   student1: {
      units: {
        unit1:true,
        unit2:true
      }
   },
   student2: {
     units:{
       unit3:true,
       unit4:true
     }
   }
}
Units: {
  unit1: {
    student: student1
    english: 80,
    math: 40,
  },
  unit2: {
    student: student1
    english: 60,
    math: 75,
  }
  unit3: {
    student: student2
    english: 63,
    math: 29,
  }
  unit4: {
    student: student2
    english: 20,
    math: 90,
  }
}

`

This organization would allow you to perform the query you want, but more importantly, other objects can reference the same instance of unit1, so now you have one source of truth for the units information so you won't have conflicting data.

TimCrowe
  • 116
  • 2
  • 6
  • Good suggestion, but I need to store for which `Unit Test` i.e. `Exam Type` the Student is scoring marks for. In your suggestion, I will need to then store `Exam Type` in the field. – Adarsh Madrecha Aug 25 '16 at 13:12
  • Moreover, I was planning to use only `REST API` to Get and Post Data. Hence if I need to perform multiple `GET` Requests (to get marks in particular Exam Type) because of DE-normalization, it will hamper User Experience. – Adarsh Madrecha Aug 25 '16 at 13:14