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

Denis picture Denis · Aug 12, 2014 · Viewed 42.4k times · Source

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"
      }
    }
  }
}

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 ?

Answer

Matt picture Matt · Mar 11, 2016

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!