4

Is it possible to query a Firestore collection for document size only? And thereby skipping the reads of all documents in a collection? I need to know the size of a collection to make pagination showing page x of x. I wish to avoid using unnecessary read quotas just to get the size of a collection.

Michael Falck Wedelgård
  • 2,943
  • 1
  • 27
  • 39
  • I had written an answer before I realized that Dan had answered this (better) before. I linked (and closed against) his answer and recommend you use that. – Frank van Puffelen Dec 05 '17 at 15:25

1 Answers1

3

There is no way to just get the number of documents in a collection. The only way to determine this is to download the documents and count them, a wasteful operation in your scenario.

If you need this, I recommend you keep a separate counter property somewhere and update that as you add/remove documents to and from the collection.

Doing this in Cloud Functions is a quite natural fit. I recommend that you have a look at the sample of keeping a counter. While it is written for Firebase's Realtime Database, the same approach works for Cloud Firestore. If you're having trouble making this work, post a new question with the minimal code where you are stuck.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807