Dickson Afful
Dickson Afful

Reputation: 838

Is there any cost efficient way to find the number of docs in a collection in firestore

I'm new to firebase and I'm trying to integrate it into my vuejs project. if I just want the number of docs in a collection instead of the actual docs, i read all the docs and use snapshot.size() to find the number. Is this cost-efficient? If not is there a better way to approach it?

Upvotes: 1

Views: 41

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138814

i read all the docs and use snapshot.size()

As the docs stays, this is what snapshot.size() is ment for. So you can go ahead with it.

If not is there a better way to approach it?

Yes it is, you can implement distributed counters as explained in the official documentation of distributed counters.

As a personal hint, don't store this kind of counters in Cloud Firestore, because every time you increase or decrease the counter will cost you a read or a write operation. Host this counter in Firebase realtime database "at almost no cost".

Upvotes: 1

Related Questions