Jump to Content
Databases

Powering up Firestore to COUNT() cost-efficiently

October 19, 2022
Joseph (JD) Batchik

Software Engineer

Denver Coneybeare

Software Engineer

Frequently, your application may need to count the number of matches for a given query. For example, if you're developing a social application, you may need to count the number of friends an individual has. 

With today's Preview launch of count(), you can easily and cost-efficiently perform a count() directly in Firestore. It’s accessible via all our server, client SDKs, Google Cloud and Firebase console.

https://storage.googleapis.com/gweb-cloudblog-publish/images/count-dropdown.max-1500x1500.jpg

Use Cases

Support for count() now enables you to utilize Firestore for building applications which need aggregation support eg. a dashboard displaying the number of active players in a gaming app or counting the number of friends on a social media app etc., without reading all the docs.  

Count() can be leveraged to simply count the number of items in a collection or to count the results based on certain conditions in any application.

For example, Let’s say you have a collection called “employees”. 

  1. To get a count() of total number of employees, you run the below query:

Loading...

Now, let’s say you want to get a total count of “developers” among your employees. 

  1. To get a count of total developers among your employees,you run the below query:

Loading...

How does COUNT work?

Firestore computes count() based on index entry matches. 

Before we discuss the methodology used by the query planner, it’s important to familiarize yourself with two new terms:

  • Index entries scanned: Number of index entries scanned to execute a query. 

  • Index entries matched: Number of index entries that match the query. 

When a query with a count() function is executed, we scan relevant index entries, and the index entries that match the query are then counted on the server. Since the matching documents never have to be retrieved, the performance of the query will primarily depend on the size of the number of index entries scanned. 

Pricing for COUNT()

Firestore's count() isn’t just easy to use, it is also cost-efficient! 

You’ll be charged for the number of index entries matched during the computation. 

Index entry matches will map to the existing Doc Reads SKU, where up to 1000 index entry matches will be equal to 1 doc read, 1,001 to 2,000 index entry matches will be equal to 2 document reads, etc.

For example, a query utilizing a count aggregation function resulting in 1500 index entry matches, will be charged 2 document reads.

Count () is also available for free-tier users. Since it is charged using document reads, it adheres to the existing free-tier quotas. 

COUNT() behavior 

For web and mobile users, during the Preview, count() will be an online only experience. You can use the count() function on any query when you are online. Support for real time listeners, and offline access to the count() function is not available at the moment. 

The count() function can be used with all our existing query patterns, including transactions. 

Next Steps 

Please refer to the documentation for more information.

With the ease and cost-efficiency of this feature, we hope you’ll agree this is the one that really counts. Get counting now.

Posted in