Arrayfunktionen
| Name | Beschreibung |
ARRAY
|
Gibt ein ARRAY mit einem Element für jedes Eingabeargument zurück.
|
ARRAY_CONCAT
|
Verkettet mehrere Arrays zu einem einzelnen ARRAY
|
ARRAY_CONTAINS
|
Gibt TRUE zurück, wenn ein bestimmter ARRAY einen bestimmten Wert enthält.
|
ARRAY_CONTAINS_ALL
|
Gibt TRUE zurück, wenn alle Werte in ARRAY vorhanden sind.
|
ARRAY_CONTAINS_ANY
|
Gibt TRUE zurück, wenn einer der Werte in ARRAY vorhanden ist.
|
ARRAY_GET
|
Gibt das Element an einem bestimmten Index in einem ARRAY zurück.
|
ARRAY_LENGTH
|
Gibt die Anzahl der Elemente in einem ARRAY zurück.
|
ARRAY_REVERSE
|
Kehrt die Reihenfolge der Elemente in einem ARRAY um.
|
SUM
|
Gibt die Summe aller NUMERIC-Werte in einem ARRAY zurück.
|
JOIN
|
Erstellt eine Verkettung der Elemente in einem ARRAY als STRING-Wert.
|
ARRAY
Syntax:
array(values: ANY...) -> ARRAY
Beschreibung:
Erstellt ein Array aus den angegebenen Elementen.
- Wenn ein Argument nicht vorhanden ist, wird es im resultierenden Array durch
NULLersetzt.
Beispiele:
| Werte | 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
Syntax:
array_concat(arrays: ARRAY...) -> ARRAY
Beschreibung:
Verkettet zwei oder mehr Arrays zu einem einzelnen ARRAY.
Beispiele:
| 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
Syntax:
array_contains(array: ARRAY, value: ANY) -> BOOLEAN
Beschreibung:
Gibt TRUE zurück, wenn value in array gefunden wird, andernfalls FALSE.
Beispiele:
| array | Wert | array_contains(array, value) |
|---|---|---|
| [1, 2, 3] | 2 | wahr |
| [[1, 2], [3]] | [1, 2] | wahr |
| [1, null] | null | wahr |
| "abc" | ALLE | Fehler |
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
Syntax:
array_contains_all(array: ARRAY, search_values: ARRAY) -> BOOLEAN
Beschreibung:
Gibt TRUE zurück, wenn alle search_values in der array gefunden werden, andernfalls FALSE.
Beispiele:
| array | search_values | array_contains_all(array, search_values) |
|---|---|---|
| [1, 2, 3] | [1, 2] | wahr |
| [1, 2, 3] | [1, 4] | falsch |
| [1, null] | [null] | wahr |
| [NaN] | [NaN] | wahr |
| [] | [] | wahr |
| [1, 2, 3] | [] | wahr |
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
Syntax:
array_contains_any(array: ARRAY, search_values: ARRAY) -> BOOLEAN
Beschreibung:
Gibt TRUE zurück, wenn einer der search_values in der array gefunden wird, andernfalls FALSE.
Beispiele:
| array | search_values | array_contains_any(array, search_values) |
|---|---|---|
| [1, 2, 3] | [4, 1] | wahr |
| [1, 2, 3] | [4, 5] | falsch |
| [1, 2, null] | [null] | wahr |
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
Syntax:
array_get(array: ARRAY, index: INT64) -> ANY
Beschreibung:
Gibt das Element am 0-basierten index in array zurück.
- Wenn
indexnegativ ist, wird auf Elemente vom Ende des Arrays aus zugegriffen, wobei-1das letzte Element ist. - Wenn
arraynicht vom TypARRAYist, gibt die Funktion einen fehlenden Wert zurück. - Wenn
indexaußerhalb des gültigen Bereichs liegt, gibt die Funktion einen fehlenden Wert zurück. - Wenn
indexnicht vom TypINT64ist, gibt die Funktion einen Fehler zurück.
Beispiele:
| array | Index | array_get(array, index) |
|---|---|---|
| [1, 2, 3] | 0 | 1 |
| [1, 2, 3] | -1 | 3 |
| [1, 2, 3] | 3 | nicht vorhanden |
| [1, 2, 3] | -4 | nicht vorhanden |
| "abc" | 0 | nicht vorhanden |
| null | 0 | nicht vorhanden |
Array |
„a“ | Fehler |
Array |
2 | Fehler |
ARRAY_LENGTH
Syntax:
array_length(array: ARRAY) -> INT64
Beschreibung:
Gibt die Anzahl der Elemente in array zurück.
Beispiele:
| 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
Syntax:
array_reverse(array: ARRAY) -> ARRAY
Beschreibung:
Kehrt die angegebene array um.
Beispiele:
| 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
Syntax:
sum(array: ARRAY) -> INT64 | FLOAT64
Beschreibung:
Gibt die Summe aller NUMERIC-Werte in einem ARRAY zurück.
- Nicht numerische Werte im Array werden ignoriert.
- Wenn ein numerischer Wert im Array
NaNist, gibt die FunktionNaNzurück. - Der Rückgabetyp wird durch den breitesten numerischen Typ im Array bestimmt:
INT64<FLOAT64. - Wenn ein 64-Bit-Ganzzahlüberlauf auftritt, bevor ein Gleitkommawert summiert wird, wird ein Fehler zurückgegeben. Wenn Gleitkommawerte summiert werden, führt ein Überlauf zu „+/- unendlich“.
- Wenn das Array überhaupt keine numerischen Werte enthält, gibt die Funktion
NULLzurück.
Beispiele:
| array | 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] | Fehler |
| [INT64.MAX_VALUE, 1, -1.0] | Fehler |
| [INT64.MAX_VALUE, 1.0] | 9.223372036854776e+18 |
Beitreten
Syntax:
join[T <: STRING | BYTES](array: ARRAY<T>, delimiter: T) -> STRING
join[T <: STRING | BYTES](array: ARRAY<T>, delimiter: T, null_text: T) -> STRING
Beschreibung:
Gibt eine Verkettung der Elemente in array als STRING zurück. array kann vom Datentyp STRING oder BYTES sein.
- Alle Elemente in
array,delimiterundnull_textmüssen denselben Typ haben. Sie müssen alleSTRINGoder alleBYTESsein. - Wenn
null_textangegeben ist, werden alleNULL-Werte inarraydurchnull_textersetzt. - Wenn
null_textnicht angegeben ist, werdenNULL-Werte inarrayaus dem Ergebnis ausgeschlossen.
Beispiele:
Wenn null_text nicht angegeben ist:
| array | delimiter | 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'] | „,“ | Fehler |
| ["a", "c"] | b',' | Fehler |
| [b'a', b'c'] | „,“ | Fehler |
Wenn null_text angegeben wird:
| array | delimiter | 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' | Fehler |
| [b'a', null] | b',' | „N“ | Fehler |