1

I have a Flutter app that successfully shows a ListView of documents within a collection. But now I need certain filters.

I know there's no OR clause in Firestore (although I think there should be), so I've read that I can achieve similar results making two or more queries depending on how many values of the OR conditions I need.

Ok, I understand the logic, but I refuse to believe this is an optimal solution. Despite that I decided to implement that solution into my app, but still can't achieve success.

Imagine a scenario: I have a collection of "employee" documents. A lot. And there are 4 types of Employee:

[Production, Administrative, Security, External]

My app pretends to allow filters depending on the type of employee. So if at certain moment I want only Production and Administrative types I would have to:

 Query q1 = collectionReference.where('type', isEqualTo: 'Production').orderBy('name');
 Query q2 = collectionReference.where('type', isEqualTo: 'Administrative').orderBy('name');
 QuerySnapshot querySnapshot1 = await q1.getDocuments();
 QuerySnapshot querySnapshot2 = await q2.getDocuments();

...but then How I can merge the two queries into one?.

And, since I have thousands of employees in the Database I paginate them using the limit(20) and startAfter(Document) clauses. The ideal is that the snapshot gives me the right number of documents, with the right conditionals and with the right order (name).


Addendum: In Java you can try this below, but what about Dart?

Task task1 = employees.whereLessThan("type", "Production") .orderBy("name") .get(); 
Task task2 = employees.whereGreaterThan("type", "Production") .orderBy("name") .get(); 
Task<List<QuerySnapshot>> allTasks = Tasks.whenAllSuccess(task1, task2); 
allTasks.addOnSuccessListener(new OnSuccessListener<List<QuerySnapshot>>() { 
   @Override 
   public void onSuccess(List<QuerySnapshot> querySnapshots) {
      ... 
   } 
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Oscarreño
  • 396
  • 4
  • 7
  • 1
    I think your question is acknowledging that the [lack of logical OR](https://stackoverflow.com/questions/46632042/how-to-perform-compound-queries-with-logical-or-in-cloud-firestore) in queries is forcing you into this situation, which is undesirable, but absolutely required. Your only recourse (without using a different db) for now is to use your workaround, and file a feature request with Firebase support. https://support.google.com/firebase/contact/support – Doug Stevenson Sep 17 '19 at 00:38
  • Thank you Doug for your quick response, but I haven't succeded with the workaround, tho. – Oscarreño Sep 17 '19 at 00:54
  • So you're having trouble 1) executing N queries, 2) waiting for them each to complete, 3) collecting the results into a single list, 4) sorting that entire list in the app as needed? – Doug Stevenson Sep 17 '19 at 01:01
  • Yes, the problems are in 3 and 4. – Oscarreño Sep 17 '19 at 01:13
  • Please edit your question to include the minimal code that reproduces what you tried for merging the query results. Posting significant code in comments is quite unreadable, as you may have noticed. – Frank van Puffelen Sep 17 '19 at 05:17
  • @FrankvanPuffelen fair enough, Thank you for your comment. – Oscarreño Sep 17 '19 at 18:56
  • You'll need call `getDocuments()` to get the documents for each query, and then loop over the [`documents`](https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/QuerySnapshot/documents.html) in the results (as shown [here]](https://stackoverflow.com/a/56024162), [here](https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/example/lib/main.dart#L39), and [here](https://stackoverflow.com/a/55176870), and in there add them to the `List` if they're not in there already. – Frank van Puffelen Sep 18 '19 at 06:12
  • Thank you very much Puf. Your suggestion is very doable. This leaves something out, I think: The order of the results must be worked on the client side, and when paginating we cannot ask for the next 10, 15 documents using the `limit`and the `startAfter(doc)` since there is no unique reference in "doc" in one snapshot. But no worries, I will find another way of presenting data, since now I know what are the Firebase ways of doing things. Maybe in the future I will get my way. I am equally thankful with your response either way. – Oscarreño Sep 18 '19 at 22:37

0 Answers0