0

Example:

final String campus = "BISU Calape";

private ValueEventListener valueEventListener = new ValueEventListener() {       
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        
        facultyList.clear();

        for(DataSnapshot dataSnapshot : snapshot.getChildren()){
            Faculty faculty = dataSnapshot.getValue(Faculty.class);
            faculty.setKey(dataSnapshot.getKey());
            if(faculty.getCampus().equals(campus) && faculty.getMemStatus.equals("Approved")){
                facultyList.add(faculty);
            }
        }

        if(facultyList.size() > 0){
            tv_noresult.setVisibility(View.INVISIBLE);
        }else{
            tv_noresult.setVisibility(View.VISIBLE);
        }

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(FacultyActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
    }
};

This code is working so far, but what if this code will have to fetch/filter millions of records?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Vince Ybañez
  • 103
  • 10
  • Did you see the documentation on sorting and filtering data: https://firebase.google.com/docs/database/android/lists-of-data#sorting_and_filtering_data? – Frank van Puffelen Sep 18 '22 at 03:42
  • @FrankvanPuffelen I have seen the documentation but I want to query based on multiple where clauses in Firebase – Vince Ybañez Sep 18 '22 at 07:34
  • Hey Vince. What do you mean by "filter firebase data using conditions"? What are the exact conditions? Please respond using @AlexMamo – Alex Mamo Sep 18 '22 at 08:58
  • @AlexMamo I want to get all members whose campus="BISU Main Campus" and member status="Approved"..I have already used orderByChild for searching members based on their Full Name – Vince Ybañez Sep 18 '22 at 09:04
  • Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Sep 18 '22 at 14:47

1 Answers1

0

The Firebase Realtime Database doesn't support queries on multiple properties. It supports only queries on a single child property. There is however a workaround in which you can create a new field in the database that can hold both values:

Firebase-root
   |
   ---- faculties
         |
         --- $campusId
                |
                --- campus: "BISU Main Campus"
                |
                --- status: "Approved"
                |
                --- campus_status: "BISU Main Campus_Approved"

So as you see, the campus_status field combines the values that you want to filter on.

So now, the following query will work:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference facultiesRef = db.child("faculties");
Query queryByCampusAndStatus = facultiesRef.orderByChild("campus_status").equalTo("BISU Main Campus_Approved");

Unlike the Realtime database, Cloud Firestore allows compound queries. This means that you can use multiple whereEqualTo() calls. So a query like the one below it will work perfectly fine:

facultiesRef.whereEqualTo("campus", "BISU Main Campus").whereEqualTo("status", "Approved");
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193