Get the count of documents in a Firestore collection
We used to have to do a lot of weird things to be able to count documents on Firestore, like keeping track of the aggregation on separate documents.
Starting October 2022 Firebase 9.12.1
now offers a native way to solve this.
Let’s say you have a collection of users on your database, and you want to know how many users you have.
In the past, you would have created a cloud function that would add 1 to the user’s stats document every time the user was created.
Now, you don’t have to keep track of that, you can simply use the getCountFromServer()
method, for
example:
Or let’s say you want to know how many paid users you have, you can create a query and get the count from there:
Limitations
It is a good idea to keep track of the Firebase docs for the limitations on this.
But the short version is:
- Results come from the Firestore server, they skip the cached/local data.
- If it is a complex aggregation that takes more than 60 seconds it’ll error out.