Skip to main content
Version: v1.18.0

collect

Aggregate Function

collect aggregate values into array

Synopsis

collect(any) -> [any]

Description

The collect aggregate function organizes its input into an array. If the input values vary in type, the return type will be an array of union of the types encountered.

Examples

Simple sequence collected into an array:

echo '1 2 3 4' | zq -z 'collect(this)' -

=>

[1,2,3,4]

Continuous collection over a simple sequence:

echo '1 2 3 4' | zq -z 'yield collect(this)' -

=>

[1]
[1,2]
[1,2,3]
[1,2,3,4]

Mixed types create a union type for the array elements:

echo '1 2 3 4 "foo"' | zq -z 'collect(this)' -

=>

[1,2,3,4,"foo"]

Create arrays of values bucketed by key:

echo '{a:1,k:1} {a:2,k:1} {a:3,k:2} {a:4,k:2}' |
zq -z 'collect(a) by k | sort' -

=>

{k:1,collect:[1,2]}
{k:2,collect:[3,4]}