集合管理功能 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