# Estimate Firestore collection count from a small sample of documents

This article shows a method of estimating the number of documents in a Firestore collection by reading only a small number of documents. It works if all documents in the collection use Firestore auto-IDs.

Important: this article may soon be obsolete as Firestore is adding built-in functionality to count the documents in a collection. I'm leaving it up for reference.

## 1. Introduction

Firestore is a NoSQL document database by Google. Firestore organizes data into collections, with each collection storing one or more documents. It excels at running fast queries on large datasets. However, it can't do some things you might be used to from SQL databases, like aggregation queries. You can't do a COUNT(*) on a firestore collection. If you want to know the number of documents in a collection, there are only two ways:

### 3.3 Improving our algorithm

Looking only at $x_k$ could lead to suboptimal results depending on the random distribution of document IDs. Instead, we can look at all of $x_1, ..., x_k$ , do the described calculation and average the results.

## 4. Code + a practical demonstration

Here's an example implementation in JavaScript:

const D0 = '0'.charCodeAt(0);
const D9 = '9'.charCodeAt(0);
const DA = 'A'.charCodeAt(0);
const DZ = 'Z'.charCodeAt(0);
const Da = 'a'.charCodeAt(0);
const Dz = 'z'.charCodeAt(0);

/** Convert a base62 digit to a number */
function digit(char) {
const d = char.charCodeAt(0);
if (D0 <= d && d <= D9) {
return BigInt(d - D0);
}
if (DA <= d && d <= DZ) {
return BigInt(d - DA + 10);
}
if (Da <= d && d <= Dz) {
return BigInt(d - Da + 36);
}
throw new Error('invalid base62 digit: ' + char);
}

/** Convert a base62 string to a BigInt */
function id2Number(id) {
let n = 0n;
for (let i = 0; i < id.length; i++) {
n += digit(id.charAt(id.length - i - 1)) * (62n ** BigInt(i));
}
return n;
}

const max = id2Number('zzzzzzzzzzzzzzzzzzzz');

/**
* Estimate the collection size from a list
* containing the smallest few document IDs
*/
function estimateN(ids) {
let estSum = 0n;
for (let i = 0; i < ids.length; i++) {
const x = id2Number(ids[i]);
const estimate = BigInt(i) * max / x;
estSum += estimate;
}
return estSum / BigInt(ids.length);
}



This post also includes a practical demo that you can play around with: Just enter values for $k$ and $n$ below and click the big button to simulate the algorithm right in your browser!

This will generate $n$ random document IDs, sort them, get the first $k$ , and estimate $n$ using the code above. You can then see the error compared to the real value of $n$ below.

If you enter a large number, the browser tab might freeze or crash, especially on mobile... so be aware of that. Anything below a million should be fine. Btw, you can use scientific notation like 25e6!