0

I have a Json Tree for Teachers database

{  "Teachers":
        {
          "Teacher_id" : {
            "email" : "teacher1@something.com",
            "name" : "teacher1",
            "Subjects" : {0 : "Maths",
                         1 : "science"
                         },
            "Subject_Exp-in-years" :{ 0:"Maths_2",
                                      1:"Science_4"
                                    }
            },
           "Teacher_id" : {
            "email" : "teacher2@something.com",
            "name" : "teacher2",
            "Subjects" : {0 : "Geography",
                          1 : "science"
                         },
            "Subject_Exp-in-years" :{ "Geography_5",
                                      "Science_1"
                                    }
          }
}

How can I get Set of Teachers who teach Science

I have used the query

var db_ref=firebase.database().ref();
    db_ref.child("Teachers").orderByChild("Subjects").equalTo("Science")

How can I check if a value is present in an array in the database by querying functions?

Mahi Tej Gvp
  • 984
  • 1
  • 14
  • 34
  • You want to check the value using the firebase query? Or you want to check the value using an array? – UmarZaii Aug 07 '17 at 12:11
  • Is the list of subjects known when you're creating the app? Or is this something that the user can enter in the app as they see fit? – Frank van Puffelen Aug 07 '17 at 14:22
  • UmarZaii I want to check if that subject is present in subjects array through firebase methods – Mahi Tej Gvp Aug 08 '17 at 04:45
  • @Frank van Puffelen those subjects can be added dynamically by the User – Mahi Tej Gvp Aug 08 '17 at 04:47
  • 1
    In that case this is a categorization problem. To implement those on Firebase you should keep an inverted mapping, from subjects to teachers. See my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Aug 08 '17 at 15:02
  • Hi frank so how do i make an index for both experience and subject where i can query for all the teachers who teach science and have an experience of 3 or more years in science – Mahi Tej Gvp Aug 09 '17 at 06:53

2 Answers2

0

When using .orderByChild("Subjects").equalTo("Science") doesn't work because none of the Subjects node children is equal to Science. So in order to solve this, I recomand you to change a little bit your database structure like this:

{  "Teachers":
    {
      "Teacher_id" : {
        "email" : "teacher1@something.com",
        "name" : "teacher1",
        "Subjects" : {Maths: true,
             science: true
             },

As you can see i have changed the childrens of Subjects with: Maths: true and science: true. To check if a value is present under a node, just use exists() function on that node.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

You can use for loop to read each of the element in an array.

When you read them one by one, you can include if else statement to check whether the value that you want existed or not. Let,s say the value is JOHN.

var index;
var arraylist = ["a", "b", "c"];
for (index = 0; index < arraylist.length; ++index) {
    if(arraylist[index] = "JOHN") {
        console.log(arraylist[index] + "is in the array");
    };
}
UmarZaii
  • 1,355
  • 1
  • 17
  • 26