Skip to content

setUnion - Returns the union of two arrays

setUnion will return the union of both arrays, when treated as sets.

NOTE: Both arrays are treated as Sets which means

  • Duplicates are removed from both arrays
  • Order is not preserved in the resulting array
  • null is treated as an empty set.

Syntax

setUnion(array1: array<T>, array2: array<T>): array<T>

Arguments

Name Type Required Description
array1 array of type T true T must be either string, bool, number, interval, timestamp, regexp or enum
array2 array of type T true T must be either string, bool, number, interval, timestamp, regexp or enum

Example - Seeing unique jobs in flight

{
    "processing_jobs_waiting": ["job1", "job2", "job3"],
    "loading_jobs_waiting": ["job1", "job2", "job3"]
}

If we want to see all processing and loading jobs in one place, we could use arrayConcat but this will result in duplicates. For our use case, we only want to see each unique job, which means we can use setUnion:

create all_jobs from processing_jobs_waiting.setUnion(loading_jobs_waiting)

This will result in the following document:

{
    "processing_jobs_waiting": ["job1", "job2", "job3"],
    "loading_jobs_waiting": ["job1", "job2", "job3"],
    "all_jobs": ["job1", "job2", "job3"]
}