集合管理函数 collection-management-functions

关于查询收集函数

表达式语言还引入了一组查询集合的函数。 这些职能说明如下。

在以下示例中,我们使用名为“LobbyBeacon”的事件,该事件包含推送通知令牌的集合。 此页面上的示例使用如下所示的事件有效负载结构:

                {
   "_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"
}
NOTE
在以下示例中,此有效负载是使用@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens}引用的,其中“LobbyBeacon”是事件名称,并且路径的其余部分对应于上面显示的结构。

all(<condition>)函数

all​函数使用布尔表达式在给定集合上启用过滤器的定义。

<listExpression>.all(<condition>)

概念示例:​在所有应用程序用户中,您可以使用IOS 13(布尔表达式“用于IOS 13==的应用程序”)获取这些用户。 此函数的结果是包含与布尔表达式匹配项目的过滤列表(例如:应用程序用户1、应用程序用户34、应用程序用户432)。

在数据Source条件活动中,您可以检查​ all ​函数的结果是否为null。 您还可以将此​ 所有 ​函数与其他函数(如​count)组合。 有关详细信息,请参阅数据Source条件活动

使用LobbyBeacon有效负载的代码示例:

以下示例使用本页顶部显示的事件有效负载。

CAUTION
不支持在历程表达式/条件中使用体验事件。 如果您的用例需要使用体验事件,请考虑替代方法。 了解详情

示例 1

我们要检查用户是否已安装了应用程序的特定版本。 为此,我们将获得与版本为1.0的移动应用程序关联的所有推送通知令牌。 然后,我们使用​ count ​函数执行一个条件,以检查返回的令牌列表是否至少包含一个元素。

count(@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.all(currentEventField.application.version == "1.0").token}) > 0

结果为true。

示例 2

此处,我们使用​ count ​函数来检查集合中是否有推送通知令牌。

count(@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.all().token}) > 0

结果为true。

count(@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.token})

表达式的结果为​3

NOTE
  • 当​ all() ​函数中的筛选条件为空时,筛选器将返回列表中的所有元素。 但是,为了计算集合的元素数,不需要all函数。

  • currentEventField仅在处理事件集合时可用,currentDataPackField在处理数据源集合时可用,currentActionField在处理自定义操作响应集合时可用。

处理具有allfirstlast的集合时,我们会逐个循环该集合的每个元素。 currentEventFieldcurrentDataPackFieldcurrentActionField对应于正在循环的元素。

第一个(<condition>)和最后一个(<condition>)函数

first​和​ last ​函数在返回满足筛选条件的列表的第一个/最后一个元素时,也启用集合上的筛选条件的定义。

<listExpression>.first(<condition>)

<listExpression>.last(<condition>)

示例 1

此表达式返回与版本为1.0的移动应用程序关联的第一个推送通知令牌。

@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.first(currentEventField.application.version == "1.0").token}

结果为token_1

示例 2

此表达式返回与版本为1.0的移动应用程序关联的最后一个推送通知令牌。

@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.last(currentEventField.application.version == "1.0").token}

结果为token_2

at(<index>)函数

at​函数允许您根据索引引用集合中的特定元素。
索引0是集合的第一个索引。

<listExpression>.at(<index>)

示例

此表达式返回列表的第二个推送通知令牌。

@event{LobbyBeacon._experience.campaign.message.profile.pushNotificationTokens.at(1).token}

结果为token_2

AI Knowledge Reference

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(), and at() 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() or last()
  • 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, and currentActionField are only available inside their respective collection contexts
  • The all function 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 and all() with a condition? — An empty all() returns every element; a condition-based all() returns only elements matching that boolean expression.
  • Q: How do I count push notification tokens without using all()? — Call count() 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 currentDataPackField inside all(), first(), or last() 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.
recommendation-more-help
journey-optimizer-help