Gibran Shah
Gibran Shah

Reputation: 1109

Getting a firebase collection and iterating through the data: how many database reads?

I have the following code which reads from a firebase database:

db.collection(ColPath)
        .get()
        .then(data => {
          const results : any[] = [];
          if (data.docs.length > 0) {
            data.docs.forEach(doc => {
              const d = doc.data();
              results.push(d);
            });
          }
      });

My question is: how many database reads does this do?

I’m assuming getting the collection is just one read. But what about doc.data(). Does that do one read per document? If so, the forEach loop causes it to do multiple database reads. Is that correct?

Upvotes: 0

Views: 177

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

Each document read results in one read. Everything revolves around individual documents. Collections are just containers; they aren't read.

Your query will result in one read for each document matched by the query.

The foreach loop does not cause reads to happen. The call to data() does not cause reads to happen. Executing the query causes the reads to happen. If you execute the query and do nothing with the results, the documents are still read and stored in memory (the query doesn't know what you want to do with them).

Upvotes: 3

Related Questions