Skip to content

cardinality - The number of unique elements in an array

Returns the number of unique elements in the array

Syntax

Like many functions in DataPrime, cardinality supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

cardinality(array: array<T>): number
array: array<T>.cardinality(): number

Arguments

NameTypeRequiredDescription
arrayarray of type TtrueT 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 cardinality(active_ips)
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.