6
votes

The Firebase Firestore documentation says:

Get multiple documents from a collection

You can also retrieve multiple documents with one request by querying documents in a collection. For example, you can use where() to query for all of the documents that meet a certain condition, then use get() to retrieve the results:

var citiesRef = db.collection('cities');
var query = citiesRef.where('capital', '==', true).get()
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
      });
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });

If I have a collection with N documents, would I incur a N-read fee or a single read, for the above query?

Is there any way to retrieve the read/write count on a per call basis using the SDK?

As some background for my rational for asking, I have a single collection with a large number of documents (around 20,000). I want to export the entire collection's documents in the most cost efficient manner (least reads).

2

2 Answers

11
votes

Every document yielded by a query counts as a document read. If your query matches and returns N documents, it will cost N document reads.

-1
votes

The database will read from N documents in order to filter them all. But only the docs who successfully passes the 'capital', '==', true test will be downloaded locally. You don't have to be scared to have big collections of documents but when you fetch the documents you need to be sure you only get those you need because it is the download of them that slows your app.

Firebase is more efficient when you use low nested data. You want them normalized in many collections instead of subcollections

https://firebase.google.com/docs/database/ios/structure-data