集合管理功能 collection-management-functions
關於查詢收集函式
運算式語言也會引入一組函式來查詢集合。 這些函式將於下文說明。
在下列範例中,讓我們使用包含集合的事件裝載:
{
"_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"
}
all(<condition>)函式
all函式使用布林運算式啟用指定集合上篩選的定義。
<listExpression>.all(<condition>)
例如,在所有應用程式使用者中,您可以透過IOS 13 (布林運算式「IOS 13==使用的應用程式」)取得使用者。 此函式的結果是篩選的清單,其中包含符合布林值運算式的專案(例如:應用程式使用者1、應用程式使用者34、應用程式使用者432)。
在資料Source條件活動中,您可以檢查 all 函式的結果是否為Null。 您也可以將此 所有 函式與其他函式(例如count)結合。 如需詳細資訊,請參閱資料Source條件活動。
範例 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。
-
當 all() 函式中的篩選條件為空白時,篩選器會傳回清單中的所有元素。 但是,若要計算集合的元素數目,不需要all函式。
-
currentEventField僅適用於操控事件集合、currentDataPackField適用於操控資料來源集合,以及currentActionField適用於操控自訂動作回應集合。處理具有
all、first和last的集合時,我們會逐一在集合的每個元素上回圈。currentEventField、currentDataPackField和currentActionField對應到正在回圈的元素。
第一個(<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。