I have list of firestore documents ids stored in players
. Now I want to get all the documents with those ids.
I can use query
and in
operator, but this is limited to 10 separate values and I expect cases with more than 10. stackoverflow
const colRef = collection(db, "Products");
const q = query(colRef, where(documentId(), 'in', players));
getDocs(q).then((snap) => {
console.log(snap.docs.map(d => ({ id: d.id, ...d.data() }))
})
I stumbled upon this question where they are using getAll
, but this function is not available in firestore 9 AFAIK.
async getUsers({userIds}) {
const refs = userIds.map(id => this.firestore.doc(`users/${id}`))
const users = await this.firestore.getAll(...refs)
console.log(users.map(doc => doc.data()))
}
I could create a list of doc reference and using Promise.all
get the data, but this will run the request in parallel, which comes with additional cost as firestore gets billed per request and not per data volume as it is in realtime database.
CodePudding user response:
they are using getAll, but this function is not available in firestore 9 AFAIK.
Yes, the getAll()
is a part of server side NodeJS SDK and not Firebase client SDK.
which comes with additional cost as firestore gets billed per request
Firestore reads are charged on number of documents fetched irrespective of how many queries you run i.e. 5 queries fetching 10 documents each will cost same amount of reads as a single query of 50 documents. At most you'll be charged for SSL overhead depending on the connection.
So one way is to execute multiple queries that'll fetch up to 10 documents each with in
operator along with Promise.all()
to run them in parallel.