Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase querying grandchildren / multilevel querying

Tags:

firebase

From everything I have read, it doesn't seem possible to query a multilevel value. My data structure looks like the following:

{
"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 key name of the dinosaurs, is there anyway to query the meta node retrieving only items 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 hacky solutions but none are scaleable, should I just flatten 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());
        })
    });
like image 453
dmo Avatar asked May 11 '15 16:05

dmo


1 Answers

Edit Jan 2016: Since this answer, Firebase has Deep Queries so you can query deeper than 1 level.

Queries can only be 1 level deep. There are a number of solutions but flattening your data and linking/referencing is an option.

In the example above you could create another node that links the user names (parent) to the dinosaurs (children) they added. Then John node can be read and immediately know which dinosaurs he added. Then be able to access other relevant data about that dino; date added, appeared,height etc.

users
  John
     bruhathkayosaurus
     Styracosaurus
     Lambeosaurus
     Spinosaurus
  Peter
     Lambeosaurus
     Seismosaurus

You will probably want to use uid's instead of names but you get the idea.

Also, it's not clear why there is a meta node in the example listed so it could be flattened thusly:

"dinosaurs": {
    "bruhathkayosaurus": {
       "addedBy":"John"
       "addedDate":"02021987"
       "appeared": -70000000
       "height": 25
    }, 
like image 89
Jay Avatar answered Sep 28 '22 18:09

Jay