1

enter image description here

I am trying to check if "1122334455" (mobile number) is a child within the "Teachers" node. Computer Science and Mechanical are both departments, and both include mobile numbers as their Childs.

I am able to check if a mobile number exists at a particular path using this

reference = FirebaseDatabase.getInstance().getReference("Person/Teachers/Computer Science");
if(snapshot.hasChild(mobile_number))
{

}

But what I want to achieve is to determine if the person exists using just their mobile number when it is not known if they're teacher or student, whether they belong to CS or mechanical. But this code does not work.

reference = FirebaseDatabase.getInstance().getReference("Person/");
if(snapshot.hasChild(mobile_number))
{

}

Also tried snapshot.child(mobile_number).exists(), that also doesn't work.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
ohmfischer
  • 21
  • 5

3 Answers3

2

Your structure makes it easy to find out the people/phone numbers for a given role (teacher/student). It does however not make it easy to find the role for a given phone number.

To allow that you'll typically want to add an additional data structure allowing the inverse lookup:

phoneNumbersRoles: {
  "1122334455": "Teachers/ComputerScience",
  "9988776655": "Teachers/ComputerScience",
  "0000055555": "Teachers/Mechanical",
}

With this structure you can look up data in both directions.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

I think you have to check for each category. "Person/Teachers/Computer Science", "Person/Teachers/Mechanical". If you want to do it in one operation, you will need to change your database format.

aruno14
  • 90
  • 8
0

In addition to @FrankvanPuffelen's answer, you can also check if a phone number exists using the following lines of code:

String phoneNumber = "1122334455";
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference teachersRef = rootRef.child("Person").child("Teachers");
teachersRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                if (ds.child(phoneNumber).exists()) {
                    Log.d(TAG, phoneNumber + " already exists in " + ds.getKey());
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be:

1122334455 already exists in Computer Science

If you need the same thing for "Students", simply change the reference to:

DatabaseReference studentsRef = rootRef.child("Person").child("Students");
studentsRef.get().addOnCompleteListener(/* ... /*);

This solution downloads the entire Teachers/Students node and verifies each child for the existence of a particular phone number. If that node will contain a huge amount of data, then FrankvanPuffelen's solution is better, as it downloads less amount of data.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193