user510164
user510164

Reputation: 646

Firestore Query chaining using AND

How to chain the query parameters for Firestore. I want to dynamically add query parameters (along with some common ones). But it does not seem to be working. Is this a firestore limitation?

Chaining in the same line works:

db.collection("MY_COLLECTION")
                .whereEqualTo("user.firebaseUserId" , FirebaseAuth.getInstance().getUid())
                .whereEqualTo("formId",formId)
                .whereEqualTo("user.active","true")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    FirestoreResponse response = new FirestoreResponse();
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                          //getting results here - works!
                        } else {

                        }
                    }
                });

But trying to add the conditions on the query object reference returns results only based on the 1st condition specified:

 Query firebaseQuery = collectionReference. whereEqualTo("user.firebaseUserId" , "myuserId"); //only this condition is applied
    firebaseQuery.whereEqualTo("user.active","true");

    if(someCondition){
         firebaseQuery.whereEqualTo("user.smart","true");
    }

firebaseQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                FirestoreResponse response = new FirestoreResponse();
                if (task.isSuccessful()) {
                    //returns results only based on the 1st condition !!
                } else {

                }
            }
        });

This is strange since the .whereEqualTo returns a Query object.

I have also tried using CollectionReference.get() - along with adding query before to CollectionReference.

Upvotes: 2

Views: 1026

Answers (1)

user510164
user510164

Reputation: 646

I figured out the issue - I was reusing the same query object "firebaseQuery " and calling whereEqualTo on the same object.

The whereEqualTo needs to be called on the query object from previous step instead of using the first query ref.

    Query firebaseQuery1 = db.collection("MY_COLLECTION")
                    .whereEqualTo("user.firebaseUserId" , "someUserId");
            Query firebaseQuery2 = firebaseQuery1.whereEqualTo("formId",formId);
            Query firebaseQuery3 = firebaseQuery2.whereEqualTo("user.active","true");

 firebaseQuery3.get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    FirestoreResponse response = new FirestoreResponse();
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                          works now !!
                            //}
                        } else {

                        }
                    }
                });

Upvotes: 3

Related Questions