Count items in a HubDB table which match a certain criteria

I want to find the total amount of items in a HubDB table where a column of type boolean is true.

I am doing it as follows, since there isn’t any native way to do this in HubL:

{% set count = 0 %}
{% for row in hubdb_table_rows(111111) %}
 {% if row.the_boolean_column %}
 {% set count = count + 1 %}
 {% endif %}
{% endfor %}
{{ count|pprint }}

But the printed value is Long: 0, though I know there are 6 items.

A more effiecient way to do this would be to simply use the |length filter and add a query to the table. For example, if you wanted to query off of a “featured” boolean, you could do something like this:

{% set query = "featured=true" %}{% set rows = hubdb_table_rows(111111, query) %}{{ rows|length }}

rows|length would be what you would consider count. You could also set it as the variable count if you wanted to use that variable name.


{% set count = rows|length %}

I believe the reason that you’re current code is coming up 0 is because of loop scoping, but this solution skips the loops altogether.

Works like a charm. Also, cool that the query works. Somewhere else I had seen this as the only way to get filtered values. Thanks!