Reputation: 530
If I use a query like below to only get the number of documents in a collection (and I am not displaying anything on screen), does that count as reading the document. So let's say if I have 1,000 documents in my products collection, does the query below result in 1000 reads? If so, what would be a cheaper option to count documents in a collection?
void countDocuments() async {
QuerySnapshot _myDoc = await Firestore.instance.collection('products').getDocuments();
_myDocCount = _myDoc.documents.length;
}
Upvotes: 1
Views: 162
Reputation: 317362
The query you're performing reads every document in the entire "products" collection. It doesn't matter that the code doesn't use the contents of the documents - it is still reading everything.
If you want a more scalable way to count document without reading the entire collection, you will need to maintain that count on your own. I suggest reading this other question for a full discussion of your options.
Upvotes: 3