Getting Started

Tutorial: Building with CleaveDB

Count & Tally

In CleaveQL, COUNT and TALLY are interchangeable synonyms for counting documents. They allow you to count total records in a bucket or count documents that have a specific field populated.

1. Counting all documents in a bucket

When invoked without a field name, COUNT and TALLY return the total number of documents:

CleaveQLExample · Count entire bucket
DISTILL FROM staff COUNT

Or with the conversational TALLY keyword:

CleaveQLExample · Using TALLY synonym
DISTILL FROM staff TALLY

Both queries return the document volume (e.g., {"count": 4} or {"tally": 4}) by reading the bucket index header in constant time.

2. Counting field presence

When passed a field name, COUNT tallies only documents where that field exists and is non-null (of any data type: strings, objects, numbers, booleans):

CleaveQLExample · Count documents with a field
DISTILL FROM staff COUNT status

You can also use the prepositional OF syntax with TALLY:

CleaveQLExample · Conversational field tally
DISTILL FROM staff TALLY OF status

Documents lacking the status property are skipped during the count.

3. Renaming counts with AS

Use AS to provide a semantic alias for your count:

CleaveQLExample · Aliased headcount
DISTILL FROM staff COUNT AS headcount

Returns: {"headcount": 4}.