简体   繁体   中英

Firebase: How to check if a child exist on different path?

在此处输入图像描述

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.

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM