Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Database - Retrieve Multiple Entries [duplicate]

My data is denormalized and uses a reverse index like suggested by the firebase team. Something similar to:

{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      // Index Ada's groups in her profile
      "groups": {
         // the value here doesn't matter, just that the key exists
         "techpioneers": true,
         "womentechmakers": true
      }
    },
    ...
  },
  "groups": {
    "techpioneers": {
      "name": "Historical Tech Pioneers",
      "members": {
        "alovelace": true,
        "ghopper": true,
        "eclarke": true
      }
    },
    ...
  }
}

Now I want to load all of Ada's groups. I can easily get a list of the group IDs that she belongs to since it's part of her user object.

Is there a way for me to query all of Ada's specific groups by ID at once? Or do I need to create a ValueEventListener per group ID and request each group separately?

On the security side - let's assume that every group is only readable by its members, so I can't query all groups and sort them after the fact.

like image 694
Itai Hanski Avatar asked Jul 13 '16 15:07

Itai Hanski


1 Answers

I guess you could do something like:

var ref = new Firebase("<..>/groups");
ref.orderByChild("members/alovelace").equalTo(true)
  .once('value').then(function(dataSnapshot) {
     // handle read data
})

(Untested, but according to this Deep-Queries are now supported)

like image 110
Timo Avatar answered Oct 24 '22 08:10

Timo