Firebase requesting grandchildren / tiered requests

From everything that I read, it is not possible to request a multilevel value. My data structure is as follows:

{ "dinosaurs": { "bruhathkayosaurus": { "meta":{ "addedBy":"John", "addedDate":"02021987" }, "appeared": -70000000, "height": 25 }, "lambeosaurus": { "meta":{ "addedBy":"Peter", "addedDate":"12041987" }, "appeared": -76000000, "height": 2.1 } } } 

Without knowing the name of the dinosaur key, you still need to request the meta node to receive only elements added by John.

In JS Something like:

  var ref = new Firebase('test.firebaseio.com/dinosaurs'); ref.orderByChild("meta/addedBy") .equalTo('Peter') .on("child_added", function(snapshot) { console.log(snapshot); }); 

There are hacker solutions, but none of them scale, should I just smooth out this data?

Edit:

I need a code review ... would this be an acceptable solution?

  var ref = new Firebase('test.firebaseio.com/dinosaurs'); ref.orderByChild("meta") .on('child_added',function(snap1){ snap1.ref().orderByChild("addedBy") .equalTo("Peter") .on('child_added', function(snap2) { console.log(snap2.val()); }) }); 
+7
firebase
source share
1 answer

Requests can only be at level 1. There are a number of solutions, but smoothing your data and snapping / snapping is an option.

In the above example, you can create another node that associates the usernames (of the parent) with the added dinosaurs (of the children). Then John node can be read and immediately find out which dinosaurs he added. Then access other relevant data about this dinosaur; Added date, height, etc.

 users John bruhathkayosaurus Styracosaurus Lambeosaurus Spinosaurus Peter Lambeosaurus Seismosaurus 

You might want to use uid instead of names, but you get the idea.

Also, it is not clear why in the above example there is a meta node so that it can be flattened this way:

 "dinosaurs": { "bruhathkayosaurus": { "addedBy":"John" "addedDate":"02021987" "appeared": -70000000 "height": 25 }, 

Change Jan 2016: With this answer, Firebase has Deep Queries, so you can request deeper than level 1.

+5
source share

All Articles