7

I am looking for a way to query by the document id instead of a field.

It might look something like that:

Firestore.instance.collection('tournaments').where(documentId, searchInput)

In this case searchInput is an incomplete documentId.

I saw a solution on Stackoverflow, but it isn't for flutter: https://stackoverflow.com/a/52252264/8539070

Another thing to keep in mind is that I am trying to display a list of all documents in a collection that match some part of the id.

Thank you for your help!

jonasxd360
  • 1,225
  • 4
  • 19
  • 35

2 Answers2

17

You can also get several documents by their id using:

Firestore.instance.collection('tournaments')..where(FieldPath.documentId, whereIn: myIdList).snapshots()

where myIdList is a List

Enjoy

NetCob
  • 171
  • 1
  • 3
5

It is not possible to have more than one document with the same document id in Cloud Firestore, which is why a query for your document id is most likely not the solution you are looking for.

You can simply fetch a single document using the document method. This returns a DocumentReference, which also allows you to get the document and listen to snapshots.

Firestore.instance.collection('tournaments').document(documentId).snapshots()
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • Sorry about my bad wording, I am trying to implement a search function and need to get all documents in a collection that have an id which includes the search string. – jonasxd360 Aug 23 '19 at 11:48
  • 1
    @jonasxd360 That is not possible if you are looking for word search. You can only use `isGreaterThanOrEqual` and `isLessThanOrEqual`. Otherwise, take a look at [this](https://firebase.google.com/docs/firestore/solutions/search). – creativecreatorormaybenot Aug 23 '19 at 12:01
  • What if I perform the search locally? Could I download all documents and then search through their id's locally? – jonasxd360 Aug 23 '19 at 12:14
  • 1
    @jonasxd360 Sure. It would be something along the lines of `documents.where((documentSnapshot) => documentSnapshot.documentID.contains(searchString)).toList()` to get a list of all documents that contain the search string in their document id. – creativecreatorormaybenot Aug 23 '19 at 15:26