ฟังก์ชันอาร์เรย์
| ชื่อ | คำอธิบาย |
ARRAY
|
แสดงผล ARRAY ที่มีองค์ประกอบ 1 รายการสำหรับอาร์กิวเมนต์อินพุตแต่ละรายการ
|
ARRAY_CONCAT
|
เชื่อมต่ออาร์เรย์หลายรายการเป็นARRAYเดียว
|
ARRAY_CONTAINS
|
แสดงผล TRUE หาก ARRAY ที่ระบุมีค่าที่ต้องการ
|
ARRAY_CONTAINS_ALL
|
แสดงผล TRUE หากค่าทั้งหมดอยู่ใน ARRAY
|
ARRAY_CONTAINS_ANY
|
แสดงผล TRUE หากมีค่าใดค่าหนึ่งอยู่ใน ARRAY
|
ARRAY_GET
|
แสดงผลองค์ประกอบที่ดัชนีที่ระบุใน ARRAY
|
ARRAY_LENGTH
|
แสดงผลจำนวนองค์ประกอบใน ARRAY
|
ARRAY_REVERSE
|
กลับลำดับขององค์ประกอบใน ARRAY
|
SUM
|
แสดงผลผลรวมของค่า NUMERIC ทั้งหมดใน ARRAY
|
JOIN
|
สร้างการเชื่อมต่อขององค์ประกอบใน ARRAY เป็นค่า STRING
|
ARRAY
ไวยากรณ์:
array(values: ANY...) -> ARRAY
คำอธิบาย:
สร้างอาร์เรย์จากองค์ประกอบที่ระบุ
- หากไม่มีอาร์กิวเมนต์ ระบบจะแทนที่ด้วย
NULLในอาร์เรย์ผลลัพธ์
ตัวอย่าง
| values | array(values) |
|---|---|
| () | [] |
| (1, 2, 3) | [1, 2, 3] |
| ("a", 1, true) | ["a", 1, true] |
| (1, null) | [1, null] |
| (1, [2, 3]) | [1, [2, 3]] |
ARRAY_CONCAT
ไวยากรณ์:
array_concat(arrays: ARRAY...) -> ARRAY
คำอธิบาย:
เชื่อมต่ออาร์เรย์ 2 รายการขึ้นไปเป็น ARRAY เดียว
ตัวอย่าง
| อาร์เรย์ | array_concat(arrays) |
|---|---|
| ([1, 2], [3, 4]) | [1, 2, 3, 4] |
| (["a", "b"], ["c"]) | ["a", "b", "c"] |
| ([1], [2], [3]) | [1, 2, 3] |
| ([], [1, 2]) | [1, 2] |
Web
const result = await execute(db.pipeline() .collection("books") .select(field("genre").arrayConcat([field("subGenre")]).as("allGenres")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayConcat([Field("subGenre")]).as("allGenres")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayConcat(field("subGenre")).alias("allGenres")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayConcat(field("subGenre")).alias("allGenres")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_concat(Field.of("subGenre")).as_("allGenres")) .execute() )
ARRAY_CONTAINS
ไวยากรณ์:
array_contains(array: ARRAY, value: ANY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากพบ value ใน array และแสดงผล FALSE ในกรณีอื่นๆ
ตัวอย่าง
| อาร์เรย์ | value | array_contains(array, value) |
|---|---|---|
| [1, 2, 3] | 2 | จริง |
| [[1, 2], [3]] | [1, 2] | จริง |
| [1, null] | Null | จริง |
| "abc" | ทั้งหมด | ข้อผิดพลาด |
Web
const result = await execute(db.pipeline() .collection("books") .select(field("genre").arrayContains(constant("mystery")).as("isMystery")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayContains(Constant("mystery")).as("isMystery")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayContains("mystery").alias("isMystery")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayContains("mystery").alias("isMystery")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_contains("mystery").as_("isMystery")) .execute() )
ARRAY_CONTAINS_ALL
ไวยากรณ์:
array_contains_all(array: ARRAY, search_values: ARRAY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากพบ search_values ทั้งหมดใน array และแสดงผล FALSE ในกรณีอื่นๆ
ตัวอย่าง
| อาร์เรย์ | search_values | array_contains_all(array, search_values) |
|---|---|---|
| [1, 2, 3] | [1, 2] | จริง |
| [1, 2, 3] | [1, 4] | เท็จ |
| [1, null] | [null] | จริง |
| [NaN] | [NaN] | จริง |
| [] | [] | จริง |
| [1, 2, 3] | [] | จริง |
Web
const result = await execute(db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll([constant("fantasy"), constant("adventure")]) .as("isFantasyAdventure") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("genre") .arrayContainsAll([Constant("fantasy"), Constant("adventure")]) .as("isFantasyAdventure") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll(listOf("fantasy", "adventure")) .alias("isFantasyAdventure") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAll(Arrays.asList("fantasy", "adventure")) .alias("isFantasyAdventure") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("genre") .array_contains_all(["fantasy", "adventure"]) .as_("isFantasyAdventure") ) .execute() )
ARRAY_CONTAINS_ANY
ไวยากรณ์:
array_contains_any(array: ARRAY, search_values: ARRAY) -> BOOLEAN
คำอธิบาย:
แสดงผล TRUE หากพบ search_values ใน array และแสดงผล FALSE ในกรณีอื่นๆ
ตัวอย่าง
| อาร์เรย์ | search_values | array_contains_any(array, search_values) |
|---|---|---|
| [1, 2, 3] | [4, 1] | จริง |
| [1, 2, 3] | [4, 5] | เท็จ |
| [1, 2, null] | [null] | จริง |
Web
const result = await execute(db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny([constant("fantasy"), constant("nonfiction")]) .as("isMysteryOrFantasy") ) );
Swift
let result = try await db.pipeline() .collection("books") .select([ Field("genre") .arrayContainsAny([Constant("fantasy"), Constant("nonfiction")]) .as("isMysteryOrFantasy") ]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny(listOf("fantasy", "nonfiction")) .alias("isMysteryOrFantasy") ) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select( field("genre") .arrayContainsAny(Arrays.asList("fantasy", "nonfiction")) .alias("isMysteryOrFantasy") ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("genre") .array_contains_any(["fantasy", "nonfiction"]) .as_("isMysteryOrFantasy") ) .execute() )
ARRAY_GET
ไวยากรณ์:
array_get(array: ARRAY, index: INT64) -> ANY
คำอธิบาย:
แสดงผลองค์ประกอบที่ดัชนี 0 ของ index ใน array
- หาก
indexเป็นค่าลบ ระบบจะเข้าถึงองค์ประกอบจากท้ายอาร์เรย์ โดย-1จะเป็นองค์ประกอบสุดท้าย - หาก
arrayไม่ใช่ประเภทARRAYฟังก์ชันจะแสดงผลค่าที่ไม่มี - หาก
indexอยู่นอกขอบเขต ฟังก์ชันจะแสดงผลค่าที่ไม่มี - หาก
indexไม่ใช่ประเภทINT64ฟังก์ชันจะแสดงผลข้อผิดพลาด
ตัวอย่าง
| อาร์เรย์ | ดัชนี | array_get(array, index) |
|---|---|---|
| [1, 2, 3] | 0 | 1 |
| [1, 2, 3] | -1 | 3 |
| [1, 2, 3] | 3 | ไม่อยู่ |
| [1, 2, 3] | -4 | ไม่อยู่ |
| "abc" | 0 | ไม่อยู่ |
| Null | 0 | ไม่อยู่ |
Array |
"ก" | ข้อผิดพลาด |
Array |
2.0 | ข้อผิดพลาด |
ARRAY_LENGTH
ไวยากรณ์:
array_length(array: ARRAY) -> INT64
คำอธิบาย:
แสดงผลจำนวนองค์ประกอบใน array
ตัวอย่าง
| อาร์เรย์ | array_length(array) |
|---|---|
| [1, 2, 3] | 3 |
| [] | 0 |
| [1, 1, 1] | 3 |
| [1, null] | 2 |
Web
const result = await execute(db.pipeline() .collection("books") .select(field("genre").arrayLength().as("genreCount")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayLength().as("genreCount")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayLength().alias("genreCount")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayLength().alias("genreCount")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_length().as_("genreCount")) .execute() )
ARRAY_REVERSE
ไวยากรณ์:
array_reverse(array: ARRAY) -> ARRAY
คำอธิบาย:
กลับค่า array ที่ระบุ
ตัวอย่าง
| อาร์เรย์ | array_reverse(array) |
|---|---|
| [1, 2, 3] | [3, 2, 1] |
| ["a", "b"] | ["b", "a"] |
| [1, 2, 2, 3] | [3, 2, 2, 1] |
Web
const result = await execute(db.pipeline() .collection("books") .select(field("genre").arrayReverse().as("reversedGenres")) );
Swift
let result = try await db.pipeline() .collection("books") .select([Field("genre").arrayReverse().as("reversedGenres")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(field("genre").arrayReverse().alias("reversedGenres")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(field("genre").arrayReverse().alias("reversedGenres")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select(Field.of("genre").array_reverse().as_("reversedGenres")) .execute() )
SUM
ไวยากรณ์:
sum(array: ARRAY) -> INT64 | FLOAT64
คำอธิบาย:
แสดงผลผลรวมของค่า NUMERIC ทั้งหมดใน ARRAY
- ระบบจะไม่สนใจค่าที่ไม่ใช่ตัวเลขในอาร์เรย์
- หากค่าตัวเลขใดในอาร์เรย์เป็น
NaNฟังก์ชันจะแสดงผลNaN - ระบบจะกำหนดประเภทการคืนค่าตามประเภทตัวเลขที่กว้างที่สุดในอาร์เรย์ ซึ่งก็คือ
INT64<FLOAT64 - หากเกิดจำนวนเต็ม 64 บิตล้นก่อนที่จะมีการรวมค่าทศนิยม ระบบจะแสดงข้อผิดพลาด หากมีการรวมค่าทศนิยม การล้นจะทำให้เกิดค่าอนันต์ +/-
- หากอาร์เรย์ไม่มีค่าตัวเลขเลย ฟังก์ชันจะแสดงผล
NULL
ตัวอย่าง
| อาร์เรย์ | sum(array) |
|---|---|
| [1, 2, 3] | 6L |
| [1L, 2L, 3L] | 6L |
| [2000000000, 2000000000] | 4000000000L |
| [10, 20.5] | 30.5 |
| [1, "a", 2] | 3L |
| [INT64.MAX_VALUE, 1] | ข้อผิดพลาด |
| [INT64.MAX_VALUE, 1, -1.0] | ข้อผิดพลาด |
| [INT64.MAX_VALUE, 1.0] | 9.223372036854776e+18 |
เข้าร่วม
ไวยากรณ์:
join[T <: STRING | BYTES](array: ARRAY<T>, delimiter: T) -> STRING
join[T <: STRING | BYTES](array: ARRAY<T>, delimiter: T, null_text: T) -> STRING
คำอธิบาย:
แสดงผลการเชื่อมองค์ประกอบใน array เป็น STRING array อาจเป็นข้อมูลประเภท STRING หรือ BYTES
- องค์ประกอบทั้งหมดใน
array,delimiterและnull_textต้องเป็นประเภทเดียวกัน โดยต้องเป็นSTRINGทั้งหมดหรือเป็นBYTESทั้งหมด - หากระบุ
null_textระบบจะแทนที่ค่าNULLในarrayด้วยnull_text - หากไม่ได้ระบุ
null_textระบบจะไม่รวมค่าNULLในarrayไว้ในผลลัพธ์
ตัวอย่าง
เมื่อไม่ได้ระบุ null_text
| อาร์เรย์ | ตัวคั่น | join(array, delimiter) |
|---|---|---|
| ["a", "b", "c"] | "," | "a,b,c" |
| ["a", null, "c"] | "," | "a,c" |
| [b'a', b'b', b'c'] | b',' | b'a,b,c' |
| ["a", b'c'] | "," | ข้อผิดพลาด |
| ["a", "c"] | b',' | ข้อผิดพลาด |
| [b'a', b'c'] | "," | ข้อผิดพลาด |
เมื่อระบุnull_text
| อาร์เรย์ | ตัวคั่น | null_text | join(array, delimiter, null_text) |
|---|---|---|---|
| ["a", null, "c"] | "," | "MISSING" | "a,MISSING,c" |
| [b'a', null, b'c'] | b',' | b'NULL' | b'a,NULL,c' |
| [null, "b", null] | "," | "MISSING" | "MISSING,b,MISSING" |
| [b'a', null, null] | b',' | b'NULL' | b'a,NULL,NULL' |
| ["a", null] | "," | b'N' | ข้อผิดพลาด |
| [b'a', null] | b',' | "N" | ข้อผิดพลาด |