5

I am using Javascript with Firebase and I want to make a query on a value exist on the second or the third level, My DB is like that :

Users :
        -KNwBd5cF6iY9dWh0eFd :
                name: "Jon Snow"
                phones:
                    phone1: "0123456789"
                    phone2: "0123456987"

And I want to make a query on the phone1 value, I am using orderByChild to query the DB for now and my code is :

var ref = firebase.database().ref("users");
ref.orderByChild("name").equalTo("Jon Snow").on("value", function(snapshot) {
    console.log(snapshot.val());
});

But I can only use it to query on the value of the first level of values but how to make another query on the second or third level.

Morad Edwar
  • 1,030
  • 2
  • 11
  • 27
  • If I got it, what you are trying to do is a [multiple where clauses](http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase), and this is a duplicate. – adolfosrs Jul 30 '16 at 18:23
  • No, I don't want to make multiple wheres I just want make a query on the child of the child of the object – Morad Edwar Jul 30 '16 at 18:37

1 Answers1

3

Without knowing the phone id and considering your current database structure this wont be possible.

One workaround is to use the actual phone number as the key in /phones.

{ 
  users:
    userId:
       phones:
          0123456789: true
}

Then you will be able to retrieve it with:

ref.orderByChild("phones/"+phoneNumber).equalTo(true).once("value", function(snapshot) {
    console.log(snapshot.val());
});

Working jsFiddle.


This would be simple if you had the phoneId.

ref.orderByChild("phones/phone1").equalTo("0123456789").once("value", function(snapshot) {
    console.log(snapshot.val());
});

Since you don't, it turns to be a good example of how you should not be structuring a noSQL database.

noSQL databases are very flexible and its structure will depend pretty much on your application. So keep it in mind. If, for example, searching for a phoneNumber will be critical in your application and you want to have more data inside /phones/phoneNumber/ you should consider having a new separate branch to handle this in a more dedicated manner than you are doing now.

adolfosrs
  • 9,286
  • 5
  • 39
  • 67
  • 1
    Great answer Adolfo! Note that the answer you deleted was a great example of why OPs data model doesn't work. I'd recommend merging it into this answer with an explanation (one more reason why arrays are often a bad solution, especially when like this case the data is actually a set). – Frank van Puffelen Jul 30 '16 at 22:53
  • Hey @FrankvanPuffelen, we would have some indexation problem in this case right? Could it lead to performance problems? – adolfosrs Jun 07 '18 at 21:14
  • 1
    You'd index need an index on each phone. For alternative data structure to work around that, see https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jun 07 '18 at 21:18