Counting Firestore Documents: the getCountFromServer() function (2023)

Firebase announced the release of a long-awaited count function last month at FirebaseSummit last month.

And it's a big deal for your Firebase bill. Before the release, you had to read all the documents to count them, meaning you would pay $0.06 per 100,000 docs. It might not look like much, but counting documents is key to many features like pagination or analytics.

Not only does the count function costs 1,000 times less―one document read per 1,000 docs―but it's also much faster because it relies on database indexes and you don't a dedicated Cloud Function anymore!

Rowy, our Firebase CMS, computes the row count out-of-the-box:

Rowy count demo

But let's see how to do it with Firebase using code.

Using The Count Function

The count function is called getCountFromServer and takes a collection reference as a parameter:

import { 
  getFirestore, collection,
  getCountFromServer  
} from 'firebase/firestore';

const db = getFirestore();

const collectionRef = collection(db, 'test');
const snapshot = await getCountFromServer(collectionRef)

const count = snapshot.data().count

You can do the same with collection groups, as well as while using the query function:

const snapshot = await getCountFromServer(query(collectionGroupRef, where('price', <, 20.00)));
const count = snapshot.data().count;

It's that simple!

Current Limitations

The count function has a few limitations:

  1. It's only available server-side, so you need a backend or Rowy to query it.
  2. It's limited to requests that don't pass the 60-second mark. If your database is too big, you won't be able to compute the aggregate.
  3. It only works on indexed fields.

Additionally, you want to avoid re-processing the count aggregate every time you need it, so it's always a good idea to cache it in a dedicated collection for increased performance and update it each time your collection grows instead:

const count = city_snapshot.data().count

await setDoc(doc(db, "metrics", "cities_count"), {
  count
});

Example Use Case: Using Count For Data Pagination

If you build a blog, you need to avoid crashing your reader's browser by displaying all your articles at once. That's where pagination comes in.

Organizing documents in pages is a way to progressively ship content to your end-user. This is what Google's pagination looks like for example:

Google pagination example

In Firebase, you can query a given page like so:

import { query, orderBy, startAt } from "firebase/firestore";  

const blogs_per_page = 25
const page_number = 2

const q = query(blogRef, orderBy("publishedAt"), startAt(blogs_per_page * (page_number - 1)), limit(blogs_per_page));

In this example, each page has 25 articles and we query the second page.

But the question is, how many pages do we need to display all documents? That's where the count function comes in to know how many documents we need to display:

const q = await getCountFromServer(query(blogRef, where("isPublished", =, "true"), orderBy("publishedAt")));
const total_published_blogs = q.data().count

const blogs_per_page = 25
const page_count = (total_published_blogs / blogs_per_page)

Now we know we can display up to page_count pages for the user to navigate through our blog.

Use Rowy To Count Firestore Documents

Rowy offers all the benefits of a smart, no-code, collaborative spreadsheet with a low learning curve, while also offering ways to gradually gravitate toward low-code features to create more custom experiences.

The best part? We’ve got plenty of templates to get you up and running in 2 minutes! So don’t hesitate and try Rowy for free.

Get started with Rowy in minutes

Continue reading

Browse all