Solution 1 :
Firestore supports OR operator by IN operator (check this). In Java API this is whereIn
method.
Please check reference here.
Good Luck!
Problem :
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();
}
}
}
}
});
Comments
Comment posted by Firestore – Merging two queries locally
Check this one:
Comment posted by Frank van Puffelen
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.
Comment posted by James Gitonga
@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
Comment posted by developers.google.com/android/reference/com/google/android/gms/…
The name
Comment posted by Frank van Puffelen
The