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:
DISTILL FROM staff COUNTOr with the conversational TALLY keyword:
DISTILL FROM staff TALLYBoth 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):
DISTILL FROM staff COUNT statusYou can also use the prepositional OF syntax with TALLY:
DISTILL FROM staff TALLY OF statusDocuments lacking the status property are skipped during the count.
3. Renaming counts with AS
Use AS to provide a semantic alias for your count:
DISTILL FROM staff COUNT AS headcountReturns: {"headcount": 4}.
