any_value
Description
Returns any non-null value from the specified expression within a group.
If no expression is provided, it defaults to the $d object.
Returns null if all values in the group are null.
Syntax
any_value(expression: any?): any
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
expression | any | false | Expression to evaluate. Defaults to $d. |
Example
Use case: Select any non-null username from a group of documents
Suppose you want to group documents by team and retrieve any available username.
Example data
{
"userId": 1,
"team": "A Team",
"username": null
},
{
"userId": 2,
"team": "A Team",
"username": "Ryan"
},
{
"userId": 3,
"team": "A Team",
"username": "Anastasia"
}
Example query
groupby team aggregate any_value(username) as random_username
Example output
The output may vary depending on the order in which documents are processed:
{
"team": "A Team",
"random_username": "Ryan"
}