Retrieving Multiple Documents in Firestore with Single Network Call
Firestore provides an efficient method for retrieving multiple documents with a single round trip. This can significantly optimize performance when dealing with a large number of document fetches.
Node.js Approach
In Node.js, you can use the getAll() method to retrieve multiple documents:
const {Firestore} = require('@google-cloud/firestore'); const firestore = new Firestore(); const documentRefs = [ firestore.doc('col/doc1'), firestore.doc('col/doc2'), ]; firestore.getAll(...documentRefs).then(docs => { docs.forEach(doc => { console.log(`Document: ${doc.id}, Data: ${JSON.stringify(doc.data())}`); }); });
This method returns a promise that resolves to an array containing the resulting document snapshots.
In Queries Support
Cloud Firestore now supports IN queries, which provides another efficient way to retrieve multiple documents:
const inClause = firestore.FieldPath.documentId(), 'in', ["123", "456", "789"]; const query = firestore.collection('myCollection').where(inClause); const docs = await query.get();
The above is the detailed content of How Can I Efficiently Retrieve Multiple Firestore Documents with a Single Network Call?. For more information, please follow other related articles on the PHP Chinese website!