Android Firebase数据库查询选择条件

时间:2016-11-11 12:43:27

标签: android firebase firebase-realtime-database query-string

我想SELECT * FROM products WHERE 'birthday' = true; 但我可以对Firebase数据库执行相同的操作吗?

我试过了

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref. child("products").orderByChild("birthday").equalTo(true).addChildEventListener ........

Firebase Database image

但它没有用。请帮忙。 如果我的数据结构不好,请同时告知如何创建更好的结构。

1 个答案:

答案 0 :(得分:3)

数据库的屏幕截图显示birthday不是产品节点的直接子项:还有一个额外的步骤,for_purpose

您的查询必须严格遵循架构,因此请更新您的查询代码:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("products")
    .orderByChild("for_purpose/birthday")
    .equalTo(true)
    .addChildEventListener ...

请注意,要使其有效工作,您必须为安全规则中每个可能的for_purpose子项设置索引。或者至少对于您在查询中使用的那些。索引必须与您的查询完全匹配,在for_purpose上放置索引是不够的。

对于这个问题,您至少需要以下内容:

{
  "rules": {
    "products": {
      ".indexOn": [
        "for_purpose/birthday",
        "for_purpose/anniversary",
        "for_gender/male",
        ...
      ]
    }
  }
}