Home > Web Front-end > JS Tutorial > How Can I Efficiently Retrieve Multiple Firestore Documents with a Single Network Call?

How Can I Efficiently Retrieve Multiple Firestore Documents with a Single Network Call?

Mary-Kate Olsen
Release: 2024-12-09 15:57:15
Original
487 people have browsed it

How Can I Efficiently Retrieve Multiple Firestore Documents with a Single Network Call?

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

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();
Copy after login

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!

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