'Firebase Realtime Database geting User by Email/Name in Android with Java

Screenshot of the database

I want to get a User or just a User UID via email or name. Tried to write the query, but I'm getting all users and then iterating through them and then getting the user but I think it's an expensive task. How can I get only one User/Uid from Realtime Database?

This is what I came up with (But don't think is the best way):

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("Users");

Query emailQuery = usersRef.orderByChild("email").equalTo(client.getEmail());

emailQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {

        for (DataSnapshot child : snapshot.getChildren()) {

            if (child.getValue(User.class).getEmail().equals(client.getEmail())){

                User user = child.getValue(User.class);
            }

        }

    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source