YQL Search
Map 相關操作
先假設資料格式是長這樣:
1 2 3 4 5 6 |
{ "swords" : { "steel" : "Iron Sword" , "mythril" : "Mythril Sword" } } |
也就是 colors 這個欄位是個 Map,然後值的格式是 Map<String, String>。
尋找 Map 中包含某個 key 的 document
1 |
where swords.key contains "steel" |
尋找 Map 欄位的某個 key 值為指定值的 document
這個要使用 sameElement 函式。
1 |
where swords contains sameElement(key contains "mythril" , value contains "Mythril Sword" ) |
Struct 相關操作
若是遇到 Struct 的狀況時,概念跟 Map 有些相似,因為一定程度來說 Map 可以看成是特殊的 Struct。
假設這是個武器店的 document,也就是每個 document 表示的是一家武器店販賣的物品,例如以下這個 JSON 表示這家店賣了兩種劍。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "swords" : [{ "modifier" : "normal" , // 普通的鐵劍 "steel" : "iron" , "price" : 100 }, { "modifier" : "normal" , // 普通的秘銀劍 "steel" : "mythril" , "price" : 2000 } ] } |
尋找有賣售價超過 1,000 的劍的武器店
1 |
where swords contains sameElement(price > 1000 ) |
尋找有賣售價低於 2,500 的秘銀劍的武器店
1 |
where swords contains sameElement(steel contains "mythril" , price < 2500 ) |
到這裡其實可以注意到,sameElement 的操作是對 swords 這個 array<struct> 做的,但它可以精細地比對 array 裡的每個 struct。當 sameElement() 給予複數條件時,必須要全部條件都符合的 struct 才會判定為 match。也就是說,在這個例子中,只有「普通的秘銀劍」會符合條件。
沒有留言:
張貼留言