2017-08-07 64 views
0

我有一个JSON树如何使用equalTo()查询功能对孩子是教师数据库阵列

{ "Teachers": 
     { 
      "Teacher_id" : { 
      "email" : "[email protected]", 
      "name" : "teacher1", 
      "Subjects" : {0 : "Maths", 
         1 : "science" 
         }, 
      "Subject_Exp-in-years" :{ 0:"Maths_2", 
             1:"Science_4" 
            } 
      }, 
      "Teacher_id" : { 
      "email" : "[email protected]", 
      "name" : "teacher2", 
      "Subjects" : {0 : "Geography", 
          1 : "science" 
         }, 
      "Subject_Exp-in-years" :{ "Geography_5", 
             "Science_1" 
            } 
      } 
} 

我怎样才能设置谁教科学

教师的我已经使用查询

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

如何通过查询函数来检查数据库中的数组是否存在?

+0

您想使用firebase查询来检查值吗?或者你想检查使用数组的值? – UmarZaii

+0

创建应用时是否已知主题列表?或者,这是用户可以在他们认为合适的应用中输入的东西吗? –

+0

UmarZaii我想通过firebase方法检查该科目是否存在于科目数组中 –

回答

0

使用.orderByChild("Subjects").equalTo("Science")时不起作用,因为Subjects节点子节点均不等于Science。因此,为了解决这个问题,我recomand你改变一点点这样的数据库结构:

{ "Teachers": 
    { 
     "Teacher_id" : { 
     "email" : "[email protected]", 
     "name" : "teacher1", 
     "Subjects" : {Maths: true, 
      science: true 
      }, 

正如你可以看到我已经改变了Subjects的儿童有:Maths: truescience: true。要检查节点下是否存在值,只需在该节点上使用exists()函数。

希望它有帮助。

+0

感谢答案Alex,但是如果我得到科目的孩子,我如何得到老师节点。有没有办法使用orderByChild()从老师节点 –

+0

嗨亚历克斯感谢您的答案。如何在Subjects节点上使用orderByChild检索Teacher节点。 –

+0

使用上面解释的确切数据库结构。 –

0

您可以使用for循环来读取数组中的每个元素。

当你逐一阅读它们时,你可以包含if else语句来检查你想要的值是否存在。假设值是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"); 
    }; 
}