简体   繁体   English

子数组中的查询对象

[英]Query object in child array

I am trying to use a query to retrieve data of student who are only 4 years old from my database but i cant figure out how to use Firebase to query the age (The array in each child). 我正在尝试使用查询从我的数据库中检索只有4岁的学生的数据,但是我无法弄清楚如何使用Firebase查询年龄(每个孩子的数组)。

{
  "student" : {
    "-Kv2RVDDsI-v6V7g_LBn" : [ {
      "name" : "sam",
      "age" : "6"
    }, {
      "name" : "tom",
      "age" : "4"
    }
   ],
   "-hguyu-v6V7g_LBn" : [ {
      "name" : "Tim",
      "age" : "12"
    }, {
      "name" : "tom",
      "age" : "4"
    }
   ]
  }
}

This is my code but it does no return anything. 这是我的代码,但不返回任何内容。

ref.child("student").queryOrdered(byChild: "age").queryEqual(toValue: 4).observe(.childAdded,  with: { (snapshot) in
        let value = snapshot.value
        print(value)
    }) { (error) in
        print(error.localizedDescription)
}

However, if i remove the queryOrdered(byChild: "age") it works. 但是,如果我删除了queryOrdered(byChild:“ age”),它将起作用。

Thanks. 谢谢。

You are referencing "Student" when actually your JSON database format child path name is "Students". 实际上,当您的JSON数据库格式子路径名称为“ Students”时,您正在引用“ Student”。

Also make sure that you are querying by key. 另外,请确保您要按键查询。 Refer to the Firebase documentation to see how to query by key. 请参阅Firebase文档以了解如何按键查询。 I don't understand your structure though why you have 2 children per key. 我不明白您的结构,但是为什么每个键有2个孩子。

Why not have a new key for each child? 为什么不为每个孩子准备一个新钥匙?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM