0

Basically, I want to query a collection of data that could have 100 docs or 200.000 docs. Since I want to target, let's say max of 50 in a query, will this query cost me to read the total number of docs in that collection or only the docs that meet the criteria of the query?

    const q = query(collection(db, 'Data'), where('clientId', '==', id));
David
  • 5,882
  • 3
  • 33
  • 44
Richardson
  • 1,804
  • 10
  • 38

1 Answers1

1

You are charged only for the number of documents returned by the query. If there are 50 documents where client ID matches, you'll be charged 50 reads irrespective of how many documents are present in the collection.

You can additionally limit the number of documents to be returned from the query:

const q = query(collection(db, 'Data'), where('clientId', '==', id), limit(10));

This query will charge at most 10 reads. In case your client needs to retrieve more documents, you can then use startAt() clause to fetch next 10 documents matching the query.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84