I have a document id list in my code, I want to use this id list in my new query. How can I enter multiple document id's?
StreamBuilder(
stream: Firestore.instance.collection("Dersler")
.document(dersler).collection("Kategori")
.document(kategori).collection("Test")
.snapshots(),
builder: (context, snapshot) {}
The "dersler
" string contains: DRk5uDVZwITKqYVLPGP0, E0y2PMnoM8H9WTeObSzi, bpaBAs15EuEMRAUihVC2, g31ZgqRtmIaYVxyTNvqG
CodePudding user response:
You can use the whereIn
method to filter the query by a list of document IDs. It would look something like this:
var documentIds = ['DRk5uDVZwITKqYVLPGP0', 'E0y2PMnoM8H9WTeObSzi', 'bpaBAs15EuEMRAUihVC2', 'g31ZgqRtmIaYVxyTNvqG'];
StreamBuilder(
stream: Firestore.instance.collection("Dersler")
.document(dersler).collection("Kategori")
.document(kategori).collection("Test")
.whereIn("documentId", documentIds)
.snapshots(),
builder: (context, snapshot) {}
Note: This will retrieve all documents in the "Test" collection where the "documentId" field is in the list of documentIds.
If you have questions, let me know.
CodePudding user response:
There's no way to pass multiple IDs to the document
call in your code. The closest you can get is by performing a collection group query on a path as samthecodingmanb shows in this answer: CollectionGroupQuery but limit search to subcollections under a particular document
So you'd have an in
condition on firebase.firestore.FieldPath.documentId()
with the document path prefixes for the documents you want to get. But even then you would not be able to include the Kategori
in the path, and can only do this for up to 10 documents paths (the limit on an in
condition).
If you can't get this approach to work (as I expect), your only alternative would be to perform a separate read for each document ID and merge the results in your application code.