Complete Guide: How to Get the Total Number of Documents from Cloud Firestore Collection in Golang

Learn how to Count Firestore Collection Documents using Golang

Inshal Khan
4 min readMar 2, 2023

Are you looking for ways to count the total number of documents in a Firestore collection using Golang? You’re in the right place! In this beginner-friendly guide, I’ll walk you through step-by-step how to use aggregation queries to count Firestore collection documents in Golang. Specifically, we’ll focus on using the WithCount() aggregation query, which can save you both time and resources compared to executing the full query. We’ll also explore the performance of the WithCount() query and how it scales with the number of index entries scanned. Let’s get started!

GoLang on Firebase protein diet

Aggregation Queries

What are aggregation queries?

Aggregation queries process data from multiple index entries and return a single summary value. In Firestore, they calculate a summary value for a set of documents and return the result to your application.

When to use aggregation queries?

Use aggregation queries to determine the number of documents in a collection or query, summarize data, and perform analytics, such as calculating the average, minimum, or maximum value in a dataset.

Limitations of aggregation queries

Aggregation queries rely on the existing index configuration used by your queries and scale proportionally to the number of index entries scanned. They do not support all query features, such as sorting or filtering.

Advantages of aggregation queries

Aggregation queries can be more efficient than traditional queries when working with large data sets, as they can summarize data and return a single result. They can also calculate values that are not easily determined by a simple query.

Query cost

The cost of an aggregation query depends on the number of documents scanned and the complexity of the query. It can incur costs for document reads and network bandwidth.

Example scenario

Imagine finding the average price of all products sold in a given month. By using an aggregation query, you can quickly summarize the relevant data and extract the information you need, saving time and resources compared to manually reading every document.

Use the WithCount() aggregation

import (
firestore2 "google.golang.org/genproto/googleapis/firestore/v1"
"fmt"
)

...

// Construct a Firestore query to filter documents
query := fireClient.Collection("CollectionName")

// Execute the count aggregation query
aggregationQuery, err := query.NewAggregationQuery().WithCount("count").Get(ctx)
if err != nil {
// Handle any errors that occurred while querying Firestore
fmt.Println("Error occurred while executing count aggregation query: ", err)
return
}

// Retrieve the count result from the aggregation query
totalDocuments := aggregationQuery["count"].(*firestore2.Value).GetIntegerValue()

This code shows an example of using count aggregation to retrieve the total number of documents in a Firestore collection.

First, a Firestore query is constructed to filter documents from the desired collection. Then, the count aggregation query is executed by calling WithCount() on the query and passing in "count" as a string parameter. This indicates that the resulting map from the aggregation query should have "count" as the key for the total number of documents.

The result of the aggregation query is stored in the aggregationQuery variable. To retrieve the count result, we use indexing to access the "count" key in the map and then typecast it to a Value type in google.golang.org/genproto/googleapis/firestore/v1. Finally, we call GetIntegerValue() on the Value type to retrieve the actual count as an integer, which is then stored in the totalDocuments variable.

Limitations

Here are some limitations to keep in mind when using the WithCount() aggregation query in Cloud Firestore:

  • WithCount() aggregation queries are only supported through direct server response. This means that the queries are processed only on the Cloud Firestore backend and not through your local cache or buffered updates. They also cannot be used with real-time listeners or offline queries at the moment.
  • If a WithCount() aggregation query takes longer than 60 seconds to resolve, it will result in a DEADLINE_EXCEEDED error. The speed of this operation depends on your index configuration and the size of your dataset.
  • It’s important to note that the performance of aggregation queries is based on the size of your dataset and the number of index entries scanned, rather than just the size of the result set.
  • The WithCount() aggregation query only reads from index entries and counts indexed fields. So, if you’re looking to count non-indexed fields, you’ll need to find a different method.
  • Adding an OrderBy clause to the query can limit the count to the entities where the sorting property exists. This means that the count may not include all the documents in the collection. If you’re looking to count all documents in a collection, it’s best to avoid using the OrderBy clause.

Pricing

Pricing for WithCount() depends on the number of index entries matched by the query. You are charged a small number of reads for a large number of matched entries.

--

--

Inshal Khan
Inshal Khan

Written by Inshal Khan

Operating Systems| IoT | API developer.

No responses yet