0

Hi I would like to use logical AND in the firebase firestore query and add the particular count to the array object. I would like to get the count according to my logical and condition but the query which I wrote was giving me the whole count for m=obj.getUid().More over the value "c" is not getting added to the arraylist instead 0 is getting added up there. In the end I would like to use logical and, get the count assign to the "c" that c need to be added up to an arraylist.

                            c=0;
                            obj.setUid(d.getId().toString());
                            db.collection("individuals").whereEqualTo("m",obj.getUid()).whereEqualTo("ctrApproved2","").whereEqualTo("ctrApproved","").get()
                                    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                        @Override
                                        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                                                c=queryDocumentSnapshots.size();
                                                Log.d("Lenght",String.valueOf(c));

                                        }
                                    });

                            man.add(obj.getUid()+" ("+c+")");
  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Jul 06 '22 at 09:27

1 Answers1

1

The reason c is seemingly always 0 is that it is being added to the list outside of the asynchronous task.

To fix this, you can move the call to man.add() into the onSuccess listener:

db.collection("individuals").whereEqualTo("m",obj.getUid()).whereEqualTo("ctrApproved2","").whereEqualTo("ctrApproved","").get()
    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        c=queryDocumentSnapshots.size();
        Log.d("Lenght",String.valueOf(c));
        man.add(obj.getUid()+" ("+c+")");
    }
});

I do not see any problems with your use of whereEqualTo() here. When calls to whereEqualTo() are chained together the way you did here, they do use a logical AND when combining the filters. (Source: https://firebase.google.com/docs/firestore/query-data/queries#compound_queries)

Could you perhaps check, and give us an example of, which document(s) you expect to be included and which you expect to be excluded?

Bob Hogg
  • 123
  • 4
  • I need whereEqualTo("ctrApproved2",""),whereEqualTo("ctrApproved","") but the result i am getting is only of this whereEqualTo("m",obj.getUid()). I would like to exclude if ctrApproved2,ctrApproved="yes" or "no". – Vikas Konaparthi Jul 06 '22 at 16:15