Home > Web Front-end > JS Tutorial > How Can I Efficiently Fetch Multiple Firestore Documents with Different IDs in One Request?

How Can I Efficiently Fetch Multiple Firestore Documents with Different IDs in One Request?

Patricia Arquette
Release: 2024-12-06 10:57:10
Original
956 people have browsed it

How Can I Efficiently Fetch Multiple Firestore Documents with Different IDs in One Request?

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])}`);
});
Copy after login

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])}`);
});
Copy after login

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"])
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template