0

How to get documents values of different field? For example, I have database with Message and I am storing all chat data in this collection.Now I want to fetch all the chat data between two user, sorting by timestamp.
As far as I knew from here, firebase introduced in operator as well,But it is for single field
Firestore snapshot.
enter image description here

Code snippet:

db.collection("Message")
                .whereEqualTo("from",mAuth.getCurrentUser().getUid()) //to is receiver UID
                .whereEqualTo("to",to)
                 ||    // Or "I need or in between" 
                .whereEqualTo("to",mAuth.getCurrentUser().getUid())
                .whereEqualTo("from",to)
                .orderBy("time_stamp", Query.Direction.ASCENDING).... 

In short I need to fetch data base on two field condition with or in between.
Expected output:
Fetch all the document between logged in user and receiver from firestore.(fetch data whose from is logged in and to is receiver id or from is receiver and to is logged in user).

I am a beginner, Please help me solving this issue or any workaround will do. Thanks

Kunchok Tashi
  • 2,413
  • 1
  • 22
  • 30
  • 2
    Please provide a concrete example and the result you expect to get. Besides that, if you're interested, here you can find a tutorial on how to create a complete and functional [Firestore Chat App](https://www.youtube.com/playlist?list=PLn2n4GESV0Ak1HiH0tTPTJXsOEy-5v9qb). – Alex Mamo May 08 '20 at 13:00
  • After doing some research on it,This is indeed a limitation of Firestore. I thought whereIn wil do the work. https://stackoverflow.com/questions/60474746/invalid-query-you-cannot-use-more-than-one-in-filter – Kunchok Tashi May 11 '20 at 07:33

1 Answers1

0

First fetch all the documents which has "from"==mAuth.getCurrentUser().getUid and then filter out the documents which has "to"==to. Here is the code:

db.collection("Message")
                .whereEqualTo("from",mAuth.getCurrentUser().getUid)
                .orderBy("time_stamp", Query.Direction.ASCENDING)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        List<DocumentSnapshot> allDocs = queryDocumentSnapshots.getDocuments();
                        List<DocumentSnapshot> requiredList = new ArrayList<>();
                        for(DocumentSnapshot d : allDocs){
                            if(/*Cast into required type.I'm assuming string*/((String)d.get("to")).equalsIgnoreCase(to)){
                                requiredList.add(d);
                            }
                        }
                        // requiredList contains the required documents
                        // do whatever you want here...
                    }
                });

Index database, if necessary. You can check logs for any required index.

Sumit
  • 300
  • 4
  • 11
  • I believe you misunderstood what I am asking, I want to fetch from and to in once as both anyone (to,from) can have mAuth.getCurrentUser().getUid and to. Between Thanks – Kunchok Tashi May 09 '20 at 09:40