I am trying to get multiple documents in my database that have been created with the same id. I know it's possible to do it using collectionGroup
with the code presented bellow, but I am looking for a potentially more efficient and cleaner way to do it.
I have read this question How to perform collection group query using document ID in Cloud Firestore indicating that it is essential to store the id in a field to find the document by using collectionGroup
.
In my case, documents with myId
can be in two different collections so I want to avoid using collectionGroup
because I would have to do it on two separate collections like this:
const collectionOneDocs = await firestoreInstance.get().collectionGroup('collectionOne').where('id', '==', myId).get();
const collectionTwoDocs = await firestoreInstance.get().collectionGroup('collectionTwo').where('id', '==', myId).get();
This code works for me, but I would like to do a single operation to access the documents and also avoid saving the id
as a field in the documents since it's not really necessary. So is there anyway in firestore to find all documents with a specific id
or is this already the best solution available with firestore?
Thanks a lot!