countby
Description
The countby command generates a count for each distinct value in a given expression, effectively grouping the results by that key.
Note
Unlike count, which tallies all records in a set, countby provides a per-group count based on the specified key or expression.
Syntax
Example
Use case: Count requests by HTTP status code
When analyzing logs, it's often useful to understand the distribution of response types. The countby command groups documents by status_code and counts the number of requests in each group.
Example data
{ "status_code": 200, "path": "/home" },
{ "status_code": 200, "path": "/about" },
{ "status_code": 404, "path": "/missing" },
{ "status_code": 500, "path": "/checkout" }
Example query
Example output
| status_code | request_count |
|---|---|
| 404 | 1 |
| 500 | 1 |
| 200 | 2 |
The countby command produces one record per unique status code, with a count of how many times each appears. Using asc orders the results from the smallest count to the largest.
Theme
Light