Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – querying different fields within a collection in Firestore

Posted on November 11, 2022 By

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 :

This question already has answers here:

How to perform compound queries with logical OR in Cloud Firestore?

(12 answers)
Closed 2 years ago.

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

READ  [FIXED] google play - Problem with REQUEST_INSTALL_PACKAGES, in Android
Powered by Inline Related Posts

Comment posted by developers.google.com/android/reference/com/google/android/gms/…

The name

Comment posted by Frank van Puffelen

The

Android Tags:android, firebase, google-cloud-firestore, java

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] Android studio compilation error from kotlin Android
[FIXED] uri – The correct way to create a file path on Android 10.0 Android
[FIXED] android – How to add a project as dependecy? Android
[FIXED] java – Android RecyclerView not showing images or text on fragment Android
[FIXED] android – AAPT: error: resource drawable/example (aka com.app:drawable/example) not found Android
[FIXED] parse platform – Android display gets rendered before getting values from backend Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme