Base bomb request at second / third level

I use Javascript with Firebase, and I want the query for the value to exist at the second or third level, My DB looks like this:

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

And I want to make a query by value phone1, I'm using orderByChildto query the database at the moment, 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 use it only to query the values โ€‹โ€‹of the first level of values, but how to make another request at the second or third level.

+4
source share
1 answer

Without knowing the phone identifier and given the current database structure, this will not be possible.

/phones.

{ 
  users:
    userId:
       phones:
          0123456789: true
}

:

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

jsFiddle.


, phoneId.

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

, , noSQL.

noSQL , . . , , phoneNumber , /phones/phoneNumber/, , , .

+2

Source: https://habr.com/ru/post/1649647/


All Articles