Skip to content

any_value - Select the first non-null value

Returns any non-null expression value in the group. If expression is not defined, it defaults to the $d object.

NOTE: Will return null if all values in expression group are null.

Syntax

any_value(expression: any?)

Arguments

Name Type Required Description
expression any false The expression generating a non-null value. Defaults to $d

Example - Basic usage

any_value can be used in conjunction with an aggregation, to populate a document with any non-null field.

Consider the following documents:

{
    "userId": 1,
    "team": "A Team",
    "username": "Chris"
},
{
    "userId": 2,
    "team": "A Team",
    "username": "Dave"
},
{
    "userId": 3,
    "team": "A Team",
    "username": "Anastasia"
}

If we group by the team keypath, we can then randomly select any user.

groupby team aggregate any_value(username) as random_username

Each time, this will result in a different username, depending on the order in which the data is scanned by the dataprime query engine.

{
    "team": "A Team",
    "random_username": "Dave"
}