5

Is that possible to get value based on multiple doc id ?

CollectionReference col1 =  Firestore.instance
        .collection('service');
     col1.where("title", isEqualTo:"Ac replaciment")
    .where("title",isEqualTo:"Oil Service")
        .getDocuments()

This code not give any result

 CollectionReference col1 =  Firestore.instance
            .collection('service');
         col1.where("title", isEqualTo:"Ac replaciment")

            .getDocuments()

This code i got result
But i have title with both "Ac replaciment" and "Oil Service" but when i call tougher it is not giving result
I need query like where title =="Oil Service" or title =="Ac replaciment" How todo this in firestore with flutter



When i run this code it return all data from server

CollectionReference col1 = Firestore.instance.collection('service');

    col1.where("title", isEqualTo: "Ac replaciment");

    col1.getDocuments().

but i only need to get result if title=="Ac replaciment" why this issue happening ?what is the correct code?

Midhilaj
  • 4,905
  • 9
  • 45
  • 88
  • 1
    Cloud Firestore does not support OR queries. The common workaround is to fire a separate query for each condition, and merge client-side. See https://stackoverflow.com/questions/47018212/implementing-or-in-firestore-query-firebase-firestore – Frank van Puffelen May 19 '19 at 14:27
  • I think my answer to another thread may help with your issue https://stackoverflow.com/a/72148576/13764313 – Bader-Al May 07 '22 at 01:18

1 Answers1

3

I can think of two solutions.

1. Push two separate query calls and concatenate the result.

This might result in longer data retrieval as you send two separate query to the server.

CollectionReference col1 = Firestore.instance.collection('service');
final acReplacimentList = await col1.where("title", isEqualTo: "Ac replaciment").getDocuments();
final oilServiceList = await col1.where("title", isEqualTo: "Oil Service").getDocuments();
return acReplacimentList.documents.addAll(oilServiceList.documents);

2. Filter documents locally.

This might be a faster solution but it will expose all other unnecessary documents.

CollectionReference col1 = Firestore.instance.collection('service');
final allList = await col1.getDocuments();
return allList.documents.where((doc) => doc["title"] == "Ac replaciment" || doc["title"] == "Oil Service");

UPDATED

3. Use Query Snapshots

CollectionReference col1 = Firestore.instance.collection('service');
final snapshots = col1.snapshots().map((snapshot) => snapshot.documents.where((doc) => doc["title"] == "Ac replaciment" || doc["title"] == "Oil Service"));
return (await snapshots.first).toList();
Michael Yuwono
  • 2,452
  • 1
  • 14
  • 32
  • if i am using firebase database then there is an option to perform this in one query with out loading all data . Using firestore is a wrong option? – Midhilaj May 19 '19 at 12:07
  • Is there any other way ? your 2nd method is expansive and 1st method is very slow – Midhilaj May 19 '19 at 12:09
  • What you are saying is multiple where case is not supported by firestore? – Midhilaj May 19 '19 at 12:12
  • I just had some more research. The current filter can only execute basic conditional statements. I think you'll need to call and map through query snapshots to enable a more complex filter. I'll update my answer and let's see if it suits your need. – Michael Yuwono May 19 '19 at 12:21
  • now i am doing your 1st soluction but i am not intrested to acept your answer becase i am waiting to get more answer to suit my need . sorry – Midhilaj May 19 '19 at 12:31
  • How about the third solution? It sends the query as a basic stream that is very customizable. It only calls the server once and does not expose all documents. – Michael Yuwono May 19 '19 at 12:33
  • Sorry, I don't understand the question. Please use some online grammar check or translator to help you communicate your idea. – Michael Yuwono May 19 '19 at 12:39
  • please explain, if my collection "service" contains 100 doc and when i use 3rd method is it make 100 doc read for selecting only selecting 3 doc from the list? – Midhilaj May 20 '19 at 01:32