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

后端 未结 3 1447
抹茶落季
抹茶落季 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!

    0 讨论(0)
  • 2020-11-29 23:41

    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.

    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.

    0 讨论(0)
  • 2020-11-29 23:48

    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.

    0 讨论(0)
提交回复
热议问题