json 如何在所有子项中查询具有特定值的属性的 firebase

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25272919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:29:04  来源:igfitidea点击:

How to query firebase for property with specific value inside all children

jsonfirebaseangularfire

提问by Denis

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

我有这个数据结构,其中 todos 被组织成遵循路径 /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"
      }
    }
  }
}

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 ?

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

采纳答案by Kato

There are no WHEREclauses in Firebase. Check out this threadfor some great structural tips on searching by multiple fields, this threadon database style queries, this blogon queries, and the docs.

WHEREFirebase中没有子句。查看此线程,了解有关按多个字段进行搜索的一些重要结构提示、有关数据库样式查询的此线程、有关查询的此博客以及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 prioritiesas explained in the docs. Then fetch items based on priority.

您还可以按照文档中的说明使用优先级。然后根据优先级获取项目。

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.

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

回答by Matt

Assuming you have the uid, this should be straightforward with the following:

假设您有 uid,这应该很简单,如下所示:

var uid = "simplelogin:1";
var todosRef = new Firebase("https://yourdb.firebaseio.com/todos/" + uid);
var privateTodosRef = todosRef.orderByChild("private").equalTo(true);
var privateTodos;

privateTodosRef.on("value", function(response) {
  privateTodos = response.val();
});

This should return an object with all of this user's private todos, organized by their todo key (ie "-JUAfv4_-ZUlH7JqM4WZ". If you'd like to use Angularfire you can wrap this in a $firebaseArray and assign it as follows:

这应该返回一个包含该用户所有私有待办事项的对象,由他们的待办事项键组织(即"-JUAfv4_-ZUlH7JqM4WZ"。如果您想使用 Angularfire,您可以将其包装在 $firebaseArray 中并按如下方式分配:

$scope.privateTodos = $firebaseArray(privateTodosRef);

Here's a referencefor more info on complex queries, and as mentioned in the other responses there are some nice optimizations that can be done with priorities and restructuring.

这是有关复杂查询的更多信息的参考,正如其他回复中提到的,有一些很好的优化可以通过优先级和重组来完成。

Happy coding!

快乐编码!

回答by Alexander Burakevych

All data in Firebaase can be accessed by URLs. If you go to your the Firebase dashboar to view your data model (Forge) and click on the property you are interested in you would be redirected to a URL by which you can access this particular data, e.g. https://myapp.firebaseio.com/todos/simplelogin:1/-JUAg9rVemiNQykfvvHs/private. Keeping this in mind you can access this data with AngularFire.

Firebaase 中的所有数据都可以通过 URL 访问。如果您转到 Firebase 仪表板查看您的数据模型 (Forge) 并单击您感兴趣的属性,您将被重定向到一个 URL,您可以通过该 URL 访问此特定数据,例如https://myapp.firebaseio。 com/todos/simplelogin:1/-JUAg9rVemiNQykfvvHs/private。记住这一点,您可以使用 AngularFire 访问这些数据。