Firestore: Fetching Multiple Documents with Multiple IDs in a Single Request
Firestore offers an efficient method for retrieving multiple documents with distinct IDs in a single round-trip to the database. This can significantly improve performance by minimizing the number of network calls required.
Node.js SDK
Within Node.js, you can use the getAll() method to accomplish this:
let documentRef1 = firestore.doc('col/doc1'); let documentRef2 = firestore.doc('col/doc2'); firestore.getAll(documentRef1, documentRef2).then(docs => { console.log(`First document: ${JSON.stringify(docs[0])}`); console.log(`Second document: ${JSON.stringify(docs[1])}`); });
Server SDK (Deprecated)
For the server SDK (deprecated), you can use the following method:
firestore.getAll().then(docs => { console.log(`First document: ${JSON.stringify(docs[0])}`); });
Cloud Firestore IN Queries
Firestore now supports IN queries, which provide a more direct approach for retrieving documents with multiple IDs:
myCollection.where(firestore.FieldPath.documentId(), 'in', ["123", "456", "789"])
This query will efficiently retrieve the documents with the specified IDs in a single request.
The above is the detailed content of How Can I Efficiently Fetch Multiple Firestore Documents with Different IDs in One Request?. For more information, please follow other related articles on the PHP Chinese website!