0

Hello I am fairly new to Firestore and android studio. I am trying to query two different fields within a document so as to fetch that document within a collection in Firestore. In this case the collection name is Dispatch_Records and the documents within the collection contain an auto id. The two fields within the document are driver_identity and extra_driver_identity. The query should fetch a document if the current user Id is equal to that in the field of driver_identity or extra_driver_identity in that document. I noticed that Firestore does not have an OR operator. What can I do so that I can fetch the document required? Below is the code:

 String user_id = firebaseAuth.getCurrentUser().getUid();

 firebaseFirestore.collection("Dispatch_Records").whereEqualTo("driver_identity",user_id)
                .whereEqualTo("extra_driver_identity",user_id)
                .addSnapshotListener(Deployments.this,new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                if(!queryDocumentSnapshots.isEmpty()){

                    for(DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){

                        if(doc.getType() == DocumentChange.Type.ADDED){


                            String dispatchId = doc.getDocument().getId();
                            DeploymentsConstructor deployment = doc.getDocument().toObject(DeploymentsConstructor.class).withId(dispatchId);
                            deploymentsList.add(deployment);
                            deploymentRecyclerAdapter.notifyDataSetChanged();


                        }

                    }




                }

            }
        });

James Gitonga
  • 101
  • 2
  • 10
  • 2
    Check this one: [Firestore - Merging two queries locally](https://stackoverflow.com/questions/50118345/firestore-merging-two-queries-locally) – Md. Asaduzzaman Feb 27 '20 at 10:18
  • Firestore cannot yet perform an OR query, where it checks multiple fields. You will need to fire two separate queries, and merge the results in your Android code. An efficient way to do that is in the answer that @Md.Asaduzzaman linked. – Frank van Puffelen Feb 27 '20 at 14:41
  • @FrankvanPuffelen when using Task.whenAllSuccess() to check the two Tasks that you have used to merge the two queries, how does it work? Does it mean when one Task fails it will still work or it will only execute if both Tasks become successful – James Gitonga Feb 29 '20 at 09:48
  • The name `Tasks.whenAllSuccess()` implies that the resulting task only completed when all constituent tasks succeed. If you want a result when all constituent tasks complete, use `Tasks.whenAllComplete()` instead. See https://developers.google.com/android/reference/com/google/android/gms/tasks/Tasks – Frank van Puffelen Feb 29 '20 at 14:43

1 Answers1

0

Firestore supports OR operator by IN operator (check this). In Java API this is whereIn method. Please check reference here.

Good Luck!

vitooh
  • 4,132
  • 1
  • 5
  • 16
  • 1
    The `IN` operator in Firestore is not the same as an `OR` operator. `IN` allows you to check for multiple values in a single field, while `OR` can also be used across multiple fields. Since OP is looking for the latter, the `IN` operator is of no help to them. – Frank van Puffelen Feb 27 '20 at 14:39