如何在所有子项中查询具有特定值的属性的 firebase [英] How to query firebase for property with specific value inside all children

查看:30
本文介绍了如何在所有子项中查询具有特定值的属性的 firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据结构,其中 todos 被组织成遵循路径/todos/uid/

I have this data structure, where todos are organized to follow path /todos/uid/

{
  "metausers" : {
    "simplelogin:1" : {
      "displayName" : "John Doe",
      "provider" : "password",
      "provider_id" : "1"
    },
    "simplelogin:2" : {
      "displayName" : "GI Jane",
      "provider" : "password",
      "provider_id" : "2"
    }
  },
  "todos" : {
    "simplelogin:1" : {
      "-JUAfv4_-ZUlH7JqM4WZ" : {
        "completed" : false,
        "done" : false,
        "group" : false,
        "private" : false,
        "subject" : "First"
      },
      "-JUAfveXP_sqqX32jCJS" : {
        "completed" : false,
        "done" : false,
        "group" : false,
        "private" : true,
        "subject" : "Second"
      },
      "-JUAfwXnMo6P53Qz6Fd2" : {
        "completed" : false,
        "done" : false,
        "group" : false,
        "private" : false,
        "subject" : "Third"
      }
    },
    "simplelogin:2" : {
      "-JUAg9rVemiNQykfvvHs" : {
        "completed" : false,
        "done" : false,
        "group" : false,
        "private" : false,
        "subject" : "Q first"
      },
      "-JUAgAmgPwZLPr2iH1Ho" : {
        "completed" : false,
        "done" : false,
        "group" : false,
        "private" : false,
        "subject" : "Q second"
      },
      "-JUAgBfF8f7V5R5-XgrY" : {
        "completed" : false,
        "done" : false,
        "group" : false,
        "private" : true,
        "subject" : "Q third"
      }
    }
  }
}

我想查询 todos 以获取带有 private:true 的所有记录.这可以使用 firebase (angularfire) 吗,我应该怎么做?或者我应该更多地非规范化并安排路径/private 以避免走下待办事项?

and i would like to query todos to get all records with private:true. Is this possible using firebase (angularfire) and how should i do it ? Or should i denormalize a bit more and arrange path /private to avoid of walking down todos ?

推荐答案

Firebase 中没有 WHERE 子句.查看此主题,了解一些很棒的结构按多个字段搜索的提示,此主题关于数据库样式查询,此博客 关于查询,以及 文档.

There are no WHERE clauses in Firebase. Check out this thread for some great structural tips on searching by multiple fields, this thread on database style queries, this blog on queries, and the docs.

您的第一种方法应该是对数据进行回读的方式进行分段.类似于以下内容:

Your first approach should be to segment data how it will be read back. Something like the following:

/todos/public
/todos/private
/todos/completed

您还可以利用优先级 如文档中所述.然后根据优先级获取项目.

You can also utilize priorities as explained in the docs. Then fetch items based on priority.

如果列表少于一千,如果数据正确分区,应该是这样,你可能只需要获取待办事项列表并在客户端过滤它 - 对于像这样的短集合来说是一个很好的选择,特别是使用像 Angular 这样出色的绑定库时.

If the list is less than a thousand, which it should be if data is properly partitioned, you can probably just grab the todo list and filter it at the client as well--a great option for short collections like this, particularly when working with a great binding lib like Angular.

这篇关于如何在所有子项中查询具有特定值的属性的 firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆