Skip to content

cardinality - The number of unique elements in an array

Returns the number of unique elements in the array

Syntax

cardinality(array: array<T>): number

Arguments

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

Example - Detecting new IP addresses

Consider the following document:

{
    "active_ips": ["123.45.32.1", "111.230.76.5", "9.8.7.1"]
}

Assume that we have a closed system, and we know that we should only have, at most, five users. Any more is cause for concern. We can use cardinality to count the number of unique IP addresses in our array.

create unique_ip_count from active_ips.cardinality()

This will result in the following document:

{
    "active_ips": ["123.45.32.1", "111.230.76.5", "9.8.7.1"],
    "unique_ip_count": 3
}

This can now power alerts, reports and more.