배열 함수
| 이름 | 설명 |
ARRAY
|
각 입력 인수에 대해 하나의 요소를 포함하는 ARRAY를 반환합니다.
|
ARRAY_CONCAT
|
여러 배열을 단일 ARRAY로 연결합니다.
|
ARRAY_CONTAINS
|
지정된 ARRAY에 특정 값이 포함되어 있으면 TRUE를 반환합니다.
|
ARRAY_CONTAINS_ALL
|
모든 값이 ARRAY에 있으면 TRUE를 반환합니다.
|
ARRAY_CONTAINS_ANY
|
ARRAY에 값이 있으면 TRUE를 반환합니다.
|
ARRAY_GET
|
ARRAY에서 지정된 색인의 요소를 반환합니다.
|
ARRAY_LENGTH
|
ARRAY의 요소 수를 반환합니다.
|
ARRAY_REVERSE
|
ARRAY의 요소 순서를 반대로 합니다.
|
SUM
|
ARRAY에 있는 모든 NUMERIC 값의 합계를 반환합니다.
|
JOIN
|
ARRAY에 있는 요소의 연결을 STRING 값으로 생성합니다.
|
ARRAY
구문:
array(values: ANY...) -> ARRAY
설명:
지정된 요소에서 배열을 구성합니다.
- 인수가 없으면 결과 배열에서
NULL로 대체됩니다.
예:
| 값 | 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
설명:
두 개 이상의 배열을 하나의 ARRAY로 연결합니다.
예:
| arrays | 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
설명:
value가 array에 있으면 TRUE를 반환하고, 그렇지 않으면 FALSE를 반환합니다.
예:
| 배열 | 값 | 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
설명:
모든 search_values가 array에 있으면 TRUE를 반환하고 그렇지 않으면 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
설명:
search_values 중 하나라도 array에 있으면 TRUE를 반환하고, 그렇지 않으면 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
설명:
array에서 0부터 시작하는 index의 요소를 반환합니다.
index가 음수이면 배열의 끝에서 요소에 액세스하며-1이 마지막 요소입니다.array가ARRAY유형이 아니면 함수는 누락된 값을 반환합니다.index가 범위를 벗어나면 함수는 누락된 값을 반환합니다.index가INT64유형이 아니면 함수는 오류를 반환합니다.
예:
| 배열 | index | 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 |
"a" | 오류 |
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
설명:
ARRAY에 있는 모든 NUMERIC 값의 합계를 반환합니다.
- 배열의 숫자가 아닌 값은 무시됩니다.
- 배열의 숫자 값이
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가 제공되면array의 모든NULL값이null_text로 대체됩니다.null_text가 제공되지 않으면array의NULL값이 결과에서 생략됩니다.
예:
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" | 오류 |