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

รวม

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

ชื่อ คำอธิบาย
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 ระบบจะแสดง จำนวนเอกสารทั้งหมดจากขั้นตอนก่อนหน้า

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("ratings").count().as("count"))
);
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").count().as("count")])
  .execute()

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("ratings").alias("count"))
    .execute()

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
Task<Pipeline.Snapshot> countField = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.count("ratings").alias("count"))
    .execute();
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("ratings").as_("count"))
    .execute()
)

COUNT_IF

ไวยากรณ์:

count_if(expression: BOOLEAN) -> INT64

คำอธิบาย:

แสดงจำนวนเอกสารจากขั้นตอนก่อนหน้าซึ่งexpression ประเมินเป็น TRUE

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(
    field("rating").greaterThan(4).countIf().as("filteredCount")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([
    AggregateFunction("count_if", [Field("rating").greaterThan(4)]).as("filteredCount")
  ])
  .execute()

Kotlin

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

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(
        AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCount")
    )
    .execute();
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_("filteredCount"))
    .execute()
)

COUNT_DISTINCT

ไวยากรณ์:

count_distinct(expression: ANY) -> INT64

คำอธิบาย:

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

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("author").countDistinct().as("unique_authors"))
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([AggregateFunction("count_distinct", [Field("author")]).as("unique_authors")])
  .execute()

Kotlin

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

Java

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

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("author").count_distinct().as_("unique_authors"))
    .execute()
)

SUM

ไวยากรณ์:

sum(expression: ANY) -> NUMBER

คำอธิบาย:

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

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

  • ระบบจะแปลง INTEGER เป็น DOUBLE หากแสดงเป็น INTEGER ไม่ได้

Web

const result = await execute(db.pipeline()
  .collection("cities")
  .aggregate(field("population").sum().as("totalPopulation"))
);
Swift
let result = try await db.pipeline()
  .collection("cities")
  .aggregate([Field("population").sum().as("totalPopulation")])
  .execute()

Kotlin

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

Java

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

result = (
    client.pipeline()
    .collection("cities")
    .aggregate(Field.of("population").sum().as_("totalPopulation"))
    .execute()
)

AVERAGE

ไวยากรณ์:

average(expression: ANY) -> FLOAT64

คำอธิบาย:

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

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

  • ระบบจะแปลง INTEGER เป็น DOUBLE หากแสดงเป็น INTEGER ไม่ได้

Web

const result = await execute(db.pipeline()
  .collection("cities")
  .aggregate(field("population").average().as("averagePopulation"))
);
Swift
let result = try await db.pipeline()
  .collection("cities")
  .aggregate([Field("population").average().as("averagePopulation")])
  .execute()

Kotlin

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

Java

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

result = (
    client.pipeline()
    .collection("cities")
    .aggregate(Field.of("population").average().as_("averagePopulation"))
    .execute()
)

ขั้นต่ำ

ไวยากรณ์:

minimum(expression: ANY) -> ANY

คำอธิบาย:

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

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

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

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("price").minimum().as("minimumPrice"))
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([Field("price").minimum().as("minimumPrice")])
  .execute()

Kotlin

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

Java

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

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("price").minimum().as_("minimumPrice"))
    .execute()
)

สูงสุด

ไวยากรณ์:

maximum(expression: ANY) -> ANY

คำอธิบาย:

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

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

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

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("price").maximum().as("maximumPrice"))
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([Field("price").maximum().as("maximumPrice")])
  .execute()

Kotlin

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

Java

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

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("price").maximum().as_("maximumPrice"))
    .execute()
)

FIRST

ไวยากรณ์:

first(expression: ANY) -> ANY

คำอธิบาย:

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

สุดท้าย

ไวยากรณ์:

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

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