Count objects in array where key value match

FOR each document how many active items are there.

SELECT *,  
      ARRAY_COUNT(ARRAY true FOR v IN d.items WHEN v.active = "yes" END)  AS itemscount 
FROM bucket_name AS d WHERE  d.`_ns` = "items";

You can do subquery Expression on ARRAY as follows. This allows you can do full query syntax and source of input from parent expression.

SELECT *,  
      (SELECT RAW COUNT(1) FROM d.items AS item WHERE item.active = "yes")[0] AS itemscount
FROM bucket_name AS d WHERE  d.`_ns` = "items";
3 Likes