Firebase search by child value

后端 未结 2 840
庸人自扰
庸人自扰 2020-11-29 01:24

I have the following structure on my Firebase database:

I would like to search for an user by name, last name or email but as I don\'t have the user key in the leve

2条回答
  •  孤街浪徒
    2020-11-29 02:07

    You can use equalTo() to find any child by value. In your case by name:

    ref.child('users').orderByChild('name').equalTo('John Doe').on("value", function(snapshot) {
        console.log(snapshot.val());
        snapshot.forEach(function(data) {
            console.log(data.key);
        });
    });
    

    The purpose of orderByChild() is to define the field you want to filter/search for. equalTo() can get an string, int and boolean value.

    Also can be used with auto generated keys (pushKey) too.

    You can find all the documentation here

提交回复
热议问题