ฟังก์ชันรวมข้อมูล

Aggregate

ฟังก์ชันการรวมทั้งหมดสามารถใช้เป็นนิพจน์ระดับบนสุดในระยะ aggregate(...) ได้

ชื่อ คำอธิบาย
COUNT แสดงผลจำนวนเอกสาร
COUNT_IF แสดงผลจำนวนเอกสารที่นิพจน์ประเมินได้เป็น TRUE
COUNT_DISTINCT แสดงผลจำนวนค่าที่ไม่ซ้ำกันและไม่ใช่ NULL
SUM แสดงผลรวมของค่า NUMERIC ทั้งหมด
AVERAGE แสดงผลค่าเฉลี่ยของค่า NUMERIC ทั้งหมด
MINIMUM แสดงผลค่าต่ำสุดที่ไม่ใช่ NULL
MAXIMUM แสดงผลค่าสูงสุดที่ไม่ใช่ NULL
FIRST แสดงผลค่า expression สำหรับเอกสารแรก
LAST แสดงผลค่า expression สำหรับเอกสารสุดท้าย
ARRAY_AGG แสดงผลอาร์เรย์ของค่าอินพุตทั้งหมด
ARRAY_AGG_DISTINCT แสดงผลอาร์เรย์ของค่าอินพุตที่ไม่ซ้ำกันทั้งหมด

COUNT

ไวยากรณ์:

count() -> INT64
count(expression: ANY) -> INT64

คำอธิบาย:

แสดงผลจำนวนเอกสารจากระยะก่อนหน้าซึ่ง expression ประเมินได้เป็นค่าที่ไม่ใช่ NULL หากไม่ได้ระบุ expression ระบบจะแสดงผลจำนวนเอกสารทั้งหมดจากระยะก่อนหน้า

Node.js
// Total number of books in the collection
const countOfAll = await db.pipeline()
  .collection("books")
  .aggregate(countAll().as("count"))
  .execute();

// Number of books with nonnull `ratings` field
const countField = await db.pipeline()
  .collection("books")
  .aggregate(field("ratings&q"count"))
  .execute();test.firestore.js

Web

// Total number of books in the collection
const countOfAll = await execute(db.pipeline()
  .collection("books")
  .aggregate(countAll().as("count"))
);

// Number of books with nonnull `ratings` field
const countField = await execute(db.pipeline()
  .collection("books")
  .aggregate(field(&quocount().as("count"))
);test.firestore.js
Swift
// Total number of books in the collection
let countAll = try await db.pipeline()
  .collection("books")
  .aggregate([CountAll().as("count")])
  .execute()

// Number of books with nonnull `ratings` field
let countField = try await db.pipeline()
  .collection("books")
  .aggregate([Field("ratings&q;count")])
  .execute()PipelineSnippets.swift

Kotlin

// Total number of books in the collection
val countAll = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countAll().alias("count"))
    .execute()

// Number of books with nonnull `ratings` field
val countField = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.count("ratins("count"))
    .execute()DocSnippets.kt

Java

// Total number of books in the collection
Task<Pipeline.Snapshot> countAll = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countAll().alias("count"))
    .execute();

// Number of books with nonnull <`ratings` field
T>askPipeline.Snapshot countField = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.count("ratingquot;count"))
    .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Count

# Total number of books in the collection
count_all = (
    client.pipeline().collection("books").aggregate(Count().as_("count")).execute()
)

# Number of books with nonnull `ratings` field
count_field = (
    client.pipeline()
    .collection("books")
    .aggregate(Count("ratinunt"))
    .execute()
)firestore_pipelines.py
Java
// Total number of books in the collection
Pipeline.Snapshot countAll =
    firestore.pipeline().collection("books").aggregate(countAll().as("count")).execute().get();

// Number of books with nonnull `ratings` field
Pipeline.Snapshot countField =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(count("ratings").as(&quo   .execute()
        .get();PipelineSnippets.java

COUNT_IF

ไวยากรณ์:

count_if(expression: BOOLEAN) -> INT64

คำอธิบาย:

แสดงผลจำนวนเอกสารจากระยะก่อนหน้าซึ่ง expression ประเมินได้เป็น TRUE

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(
    field("rating").greaterThan(4).countIf().as("filtered  .execute();test.firestore.js

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(
    field("rating").greaterThan(4).countIf().as(&ququot;)
  )
);test.firestore.js
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([
    AggregateFunction("count_if", [Field("rating").greaterThan(4)]).as(&quo
  ])
  .execute()PipelineSnippets.swift

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(
        AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCou)
    .execute()DocSnippets.kt

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(
        AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCoun   .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("rating").greater_than(4).count_if().as_("filteredcute()
)firestore_pipelines.py
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(countIf(field("rating").greaterThan(4)).as("filteredCount"))
      .get();PipelineSnippets.java

COUNT_DISTINCT

ไวยากรณ์:

count_distinct(expression: ANY) -> INT64

คำอธิบาย:

แสดงผลจำนวนค่าที่ไม่ซ้ำกันและไม่ใช่ NULL และไม่ใช่ ABSENT ของ expression

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(field("author").countDistinct().as("unique  .execute();test.firestore.js

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("author").countDistinct().as(&rs"))
);test.firestore.js
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([AggregateFunction("count_distinct", [Field("author")]).as(&qt;)])
  .execute()PipelineSnippets.swift

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countDistinct("author").alias("unique_)
    .execute()DocSnippets.kt

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countDistinct("author").alias("unique_a   .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("author").count_distinct().as_("unique_aucute()
)firestore_pipelines.py
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(countDistinct("author").as("unique_authors"))
      .get();PipelineSnippets.java

SUM

ไวยากรณ์:

sum(expression: ANY) -> NUMBER

คำอธิบาย:

แสดงผลรวมของค่าตัวเลขทั้งหมด โดยละเว้นค่าที่ไม่ใช่ตัวเลข แสดงผล NaN หากมีค่าใดค่าหนึ่งเป็น NaN

เอาต์พุตจะมีประเภทเดียวกับประเภทอินพุตที่กว้างที่สุด ยกเว้นในกรณีต่อไปนี้

  • INTEGER จะถูกแปลงเป็น DOUBLE หากแสดงเป็น INTEGER ไม่ได้
Node.js
const result = await db.pipeline()
  .collection("cities")
  .aggregate(field("population").sum().as("totalPo  .execute();test.firestore.js

Web

const result = await execute(db.pipeline()
  .collection("cities")
  .aggregate(field("population").sum().as(&qon"))
);test.firestore.js
Swift
let result = try await db.pipeline()
  .collection("cities")
  .aggregate([Field("population").sum().as("totalPoxecute()PipelineSnippets.swift

Kotlin

val result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.sum("population").alias("totalPop)
    .execute()DocSnippets.kt

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.sum("population").alias("totalPopu   .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("cities")
    .aggregate(Field.of("population").sum().as_("totalPopulcute()
)firestore_pipelines.py
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("cities")
        .aggregate(sum("population").as("totalPopulation"))
      .get();PipelineSnippets.java

AVERAGE

ไวยากรณ์:

average(expression: ANY) -> FLOAT64

คำอธิบาย:

แสดงผลค่าเฉลี่ยของค่าตัวเลขทั้งหมด โดยละเว้นค่าที่ไม่ใช่ตัวเลข ประเมินได้เป็น NaN หากมีค่าใดค่าหนึ่งเป็น NaN หรือ NULL หากไม่มีการรวมค่าตัวเลข

เอาต์พุตจะมีประเภทเดียวกับประเภทอินพุต ยกเว้นในกรณีต่อไปนี้

  • INTEGER จะถูกแปลงเป็น DOUBLE หากแสดงเป็น INTEGER ไม่ได้
Node.js
const result = await db.pipeline()
  .collection("cities")
  .aggregate(field("population").average().as("averagePo  .execute();test.firestore.js

Web

const result = await execute(db.pipeline()
  .collection("cities")
  .aggregate(field("population").average().as(&quoon"))
);test.firestore.js
Swift
let result = try await db.pipeline()
  .collection("cities")
  .aggregate([Field("population").average().as("averagePoxecute()PipelineSnippets.swift

Kotlin

val result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.average("population").alias("averagePop)
    .execute()DocSnippets.kt

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.average("population").alias("averagePopu   .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("cities")
    .aggregate(Field.of("population").average().as_("averagePopulcute()
)firestore_pipelines.py
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("cities")
        .aggregate(average("population").as("averagePopulation"))
      .get();PipelineSnippets.java

MINIMUM

ไวยากรณ์:

minimum(expression: ANY) -> ANY

คำอธิบาย:

แสดงผลค่าต่ำสุดที่ไม่ใช่ NULL และไม่ใช่ค่าที่ขาดหายไปของ expression เมื่อประเมินในเอกสารแต่ละฉบับ

หากไม่มีค่าที่ไม่ใช่ NULL และไม่ใช่ค่าที่ขาดหายไป ระบบจะแสดงผล NULL ซึ่งรวมถึงกรณีที่ไม่มีการพิจารณาเอกสาร

หากมีค่าต่ำสุดที่เทียบเท่ากันหลายค่า ระบบจะแสดงผลค่าใดค่าหนึ่ง การจัดลำดับประเภทค่าจะเป็นไปตามการจัดลำดับที่ระบุไว้

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(field("price").minimum().as("mini  .execute();test.firestore.js

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("price").minimum().asce"))
);test.firestore.js
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([Field("price").minimum().as("minixecute()PipelineSnippets.swift

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.minimum("price").alias("minim)
    .execute()DocSnippets.kt

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.minimum("price").alias("minimu   .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("price").minimum().as_("minimumcute()
)firestore_pipelines.py
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(minimum("price").as("minimumPrice"))
      .get();PipelineSnippets.java

MAXIMUM

ไวยากรณ์:

maximum(expression: ANY) -> ANY

คำอธิบาย:

แสดงผลค่าสูงสุดที่ไม่ใช่ NULL และไม่ใช่ค่าที่ขาดหายไปของ expression เมื่อประเมินในเอกสารแต่ละฉบับ

หากไม่มีค่าที่ไม่ใช่ NULL และไม่ใช่ค่าที่ขาดหายไป ระบบจะแสดงผล NULL ซึ่งรวมถึงกรณีที่ไม่มีการพิจารณาเอกสาร

หากมีค่าสูงสุดที่เทียบเท่ากันหลายค่า ระบบจะแสดงผลค่าใดค่าหนึ่ง การจัดลำดับประเภทค่าจะเป็นไปตามการจัดลำดับที่ระบุไว้

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(field("price").maximum().as("maxi  .execute();test.firestore.js

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("price").maximum().asce"))
);test.firestore.js
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([Field("price").maximum().as("maxixecute()PipelineSnippets.swift

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.maximum("price").alias("maxim)
    .execute()DocSnippets.kt

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.maximum("price").alias("maximu   .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("price").maximum().as_("maximumcute()
)firestore_pipelines.py
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(maximum("price").as("maximumPrice"))
      .get();PipelineSnippets.java

FIRST

ไวยากรณ์:

first(expression: ANY) -> ANY

คำอธิบาย:

แสดงผลค่าของ expression สำหรับเอกสารแรกที่แสดงผล

LAST

ไวยากรณ์:

last(expression: ANY) -> ANY

คำอธิบาย:

แสดงผลค่าของ expression สำหรับเอกสารสุดท้ายที่แสดงผล

ARRAY_AGG

ไวยากรณ์:

array_agg(expression: ANY) -> ARRAY<ANY>

คำอธิบาย:

แสดงผลอาร์เรย์ที่มีค่าทั้งหมดของ expression เมื่อประเมินในเอกสารแต่ละฉบับ

หากนิพจน์แสดงผลเป็นค่าที่ขาดหายไป ระบบจะแปลงค่าดังกล่าวเป็น NULL

ลำดับขององค์ประกอบในอาร์เรย์เอาต์พุตไม่คงที่และไม่ควรนำไปใช้

ARRAY_AGG_DISTINCT

ไวยากรณ์:

array_agg_distinct(expression: ANY) -> ARRAY<ANY>

คำอธิบาย:

แสดงผลอาร์เรย์ที่มีค่าที่ไม่ซ้ำกันทั้งหมดของ expression เมื่อประเมินในเอกสารแต่ละฉบับ

หากนิพจน์แสดงผลเป็นค่าที่ขาดหายไป ระบบจะแปลงค่าดังกล่าวเป็น NULL

ลำดับขององค์ประกอบในอาร์เรย์เอาต์พุตไม่คงที่และไม่ควรนำไปใช้