配列関数

配列関数

名前 説明
ARRAY 入力引数ごとに 1 つの要素を含む ARRAY を返します。
ARRAY_CONCAT 複数の配列を 1 つの 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 に置き換えられます。

例:

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 つ以上の配列を 1 つの 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

説明:

valuearray に含まれている場合は TRUE、それ以外の場合は FALSE を返します。

例:

配列 array_contains(array, value)
[1, 2, 3] 2 true
[[1, 2], [3]] [1, 2] true
[1, null] null true
"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_valuesarray に含まれている場合は TRUE、それ以外の場合は FALSE を返します。

例:

配列 search_values array_contains_all(array, search_values)
[1, 2, 3] [1, 2] true
[1, 2, 3] [1, 4] false
[1, null] [null] true
[NaN] [NaN] true
[] [] true
[1, 2, 3] [] true

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

説明:

arraysearch_values のいずれかが含まれている場合は TRUE を返し、それ以外の場合は FALSE を返します。

例:

配列 search_values array_contains_any(array, search_values)
[1, 2, 3] [4, 1] true
[1, 2, 3] [4, 5] false
[1, 2, null] [null] true

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 は最後の要素です。
  • arrayARRAY 型でない場合、関数は欠損値を返します。
  • index が範囲外の場合、関数は欠損値を返します。
  • indexINT64 型でない場合、この関数はエラーを返します。

例:

配列 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 のすべての要素、delimiternull_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" エラー