How to check if a certain value is there in a Firebase database

What I want is: to check if the user input value is available in the database. I have an EditText in my app, the user enters a phone number. When he clicks the button, the button requests to see in the database to see if the user input matches any record of a phone number in the database.

I haven’t done anything um lost, but I have a screenshot of my database structure.

See screenshot

Answer

To check for example, if “+263782876566” already exists in the database, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference officeRef = rootRef.child("Users").child("Office");
Query queryByPhone = officeRef.orderByChild("phone").equalTo("+263782876566");
officeRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String type = ds.child("type").getValue(String.class);
                Log.d(TAG, type);
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

So we are using a reference that points to Users -> Office and then we create a Query object to actually check within all children if the “phone” property holds a specific value. If we find some results, we simply log the type. In your particular example, the result in the logcat will be:

Office member