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

后端 未结 3 1482
抹茶落季
抹茶落季 2020-11-29 23:11

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

{
  \"metausers\" : {
    \"simplelogin:1\" : {
      \"displayName\" : \"Jo         


        
3条回答
  •  情歌与酒
    2020-11-29 23:40

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

    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:

    $scope.privateTodos = $firebaseArray(privateTodosRef);
    

    Here's a reference for 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!

提交回复
热议问题