Collection management functions collection-management-functions
About query collection functions
The expression language also introduces a set of functions to query collections. These functions are explained below.
In the following examples, we use an event named “LobbyBeacon” containing a collection of push notification tokens. The examples on this page use the event payload structure shown below:
{
"_experience":{
"campaign":{
"message":{
"profile":{
"pushNotificationTokens":[
{
"token":"token_1",
"application":{
"_id":"APP1",
"name":"MarltonMobileApp",
"version":"1.0"
}
},
{
"token":"token_2",
"application":{
"_id":"APP2",
"name":"MarketplaceApp",
"version":"1.0"
}
},
{
"token":"token_3",
"application":{
"_id":"APP3",
"name":"VendorApp",
"version":"2.0"
}
}
]
}
}
}
},
"timestamp":"1536160728"
}
@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens} where “LobbyBeacon” is the event name and the rest of the path corresponds to the structure shown above.The all(<condition>) function
The all function enables the definition of a filter on a given collection by using a boolean expression.
<listExpression>.all(<condition>)
Conceptual example: Among all the app users, you can get the ones using IOS 13 (boolean expression “app used == IOS 13”). The result of this function is the filtered list containing items matching the boolean expression (example: app user 1, app user 34, app user 432).
In a Data Source Condition activity you can check if the result of the all function is null or not. You can also combine this all function with other functions such as count. For more information, see Data Source Condition activity.
Code examples using the LobbyBeacon payload:
The examples below use the event payload shown at the top of this page.
Example 1
We want to check if a user has installed a specific version of an application. For this we get all the push notification tokens associated with mobile applications for which the version is 1.0. Then, we perform a condition with the count function to check that the returned list of tokens contains at least one element.
count(@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.all(currentEventField.application.version == "1.0").token}) > 0
The result is true.
Example 2
Here we use the count function to check if there are push notification tokens in the collection.
count(@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.all().token}) > 0
The result is true.
count(@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.token})
The result of the expression is 3.
-
When the filtering condition in the all() function is empty, the filter will return all the elements in the list. However, in order to count the number of elements of a collection, the all function is not required.
-
currentEventFieldis only available when manipulating event collections,currentDataPackFieldwhen manipulating data source collections andcurrentActionFieldwhen manipulating custom action response collections.
all, first and last, we loop on each element of the collection one by one. currentEventField, currentDataPackField and currentActionField correspond to the element being looped.The first(<condition>) and last(<condition>) functions
The first and last functions also enable the definition of a filter on the collection while returning the first/last element of the list that meets the filter.
<listExpression>.first(<condition>)
<listExpression>.last(<condition>)
Example 1
This expression returns the first push notification token associated with mobile applications for which the version is 1.0.
@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.first(currentEventField.application.version == "1.0").token}
The result is token_1.
Example 2
This expression returns the last push notification token associated with mobile applications for which the version is 1.0.
@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.last(currentEventField.application.version == "1.0").token}
The result is token_2.
The at(<index>) function
The at function allows you to reference a specific element in a collection according to an index.
Index 0 is the first index of the collection.
<listExpression>.at(<index>)
Example
This expression returns the second push notification token of the list.
@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.at(1).token}
The result is token_2.
This section contains structured knowledge intended to support interpretation, retrieval, and question answering related to this topic.
For complete understanding, this information should be combined with the documentation on this page. Neither source is intended to stand alone; the page describes the feature, while this section provides additional context that helps disambiguate terminology, intent, applicability, and constraints.
- TL;DR: This page documents the
all(),first(),last(), andat()collection management functions used in the Journey advanced expression editor, illustrated with push notification token payload examples.
Intents:
- Filter a collection of event or data source fields using a boolean condition with
all(<condition>) - Count filtered or unfiltered collection elements using
count()combined with collection functions - Retrieve the first or last matching element of a collection using
first()orlast() - Access a collection element at a specific zero-based index using
at(<index>) - Understand which loop variable (
currentEventField,currentDataPackField,currentActionField) applies to each collection context
Glossary:
- all(condition): Filters a collection and returns all items matching the given boolean expression (product-specific)
- first(condition): Returns the first (most recent for experience events) element in a collection matching the condition (product-specific)
- last(condition): Returns the last (oldest for experience events) element in a collection matching the condition (product-specific)
- at(index): Returns the element at the specified zero-based index of a collection (product-specific)
- currentEventField: Loop variable available only when iterating over event collections (product-specific)
- currentDataPackField: Loop variable available only when iterating over data source collections (product-specific)
- currentActionField: Loop variable available only when iterating over custom action response collections (product-specific)
Guardrails:
- Using experience events in journey expressions/conditions is not supported; consider alternative methods such as computed attributes
currentEventField,currentDataPackField, andcurrentActionFieldare only available inside their respective collection contexts- The
allfunction is not required to count collection elements —count()can be applied directly to the field path - When
all()is called with an empty condition, all elements in the collection are returned
Terminology:
- Canonical name: Collection Management Functions — Acronym: none — variants: collection functions, query collection functions
- Synonyms: “all()” = “collection filter function”; “at()” = “index accessor”
- Do not confuse:
first()(most recent experience event) ≠ first inserted element in general lists
FAQ:
- Q: What is the difference between
all()with an empty condition andall()with a condition? — An emptyall()returns every element; a condition-basedall()returns only elements matching that boolean expression. - Q: How do I count push notification tokens without using
all()? — Callcount()directly on the token field path, e.g.count(@event{LobbyBeacon...pushNotificationTokens.token}). - Q: Which variable do I use to reference the current element when looping over a data source collection? — Use
currentDataPackFieldinsideall(),first(), orlast()on data source collections. - Q: How do I get the second item in a collection? — Use
at(1)because index 0 is the first element. - Q: Why does
last()return the oldest experience event? — Experience events are stored in reverse chronological order, so the last position in the collection corresponds to the oldest event.