2

I'm developing a Flutter app using Firebase and Cloud Firestore.

In Firestore I have a collections of Users.

I want to perform a query who give me a list of Users with documentId in a range.

Example

My database:

> Users --> docId1 --> {name: "Domenico"}
>       --> docId2 --> {name: "Antonio"}
>       --> docId3 --> {name: "Maria"}

My desidered query is something like:

Select name from Users
where documentId in (docId2, docId3)

who gives me ["Antonio", "Maria"].

On documentation I've found how to filter by a document key but not a collection by a range of ducumentId.

Can you help me to perform this query or to understand if I'm doing something wrong?

Thank you.

ilBarra
  • 796
  • 1
  • 8
  • 25
  • 2
    There is indeed no way to build such a query to Firestore. In Javascript you would use `Promise.all()` to execute several asynchronous queries in parallel. I don't know how to do that in Dart, however. – Renaud Tarnec Nov 11 '18 at 10:37
  • PS: this may help https://stackoverflow.com/questions/42176092/dartlang-wait-more-than-one-future – Renaud Tarnec Nov 11 '18 at 10:39
  • Maybe you can save the `document id` (also) as a field in the document and perform a range query on this field – vinsce Nov 12 '18 at 10:44
  • I don't think the answer marked at the top of the page: "This question already has an answer here: [Google Firestore - how to get document by multiple ids in one round trip?](https://stackoverflow.com/questions/46721517/google-firestore-how-to-get-document-by-multiple-ids-in-one-round-trip) "helps this question since it is for server code and `firestore.getAll()` does not exist in Flutter SDK. Check this answer: [Cloud Firestore getAll() equivalent in Flutter](https://stackoverflow.com/a/54952344/874886) – aytunch Mar 01 '19 at 21:41

1 Answers1

4

You can't query on a range of id. You have to do Two distinct query

var query = firestore.collection("users").doc(docId);

But if you just want to filter on user Name you can do this

var query = firestore.collection("users").where('name', isEqualTo: 'Domenico');

This kind of query is one of the disadvantage of using firestore.

mcfly
  • 774
  • 1
  • 8
  • 18