Tổng hợp
Bạn có thể sử dụng tất cả hàm tổng hợp làm biểu thức cấp cao nhất trong giai đoạn aggregate(...).
| Tên | Mô tả |
COUNT
|
Trả về số lượng tài liệu. |
COUNT_IF
|
Trả về số lượng tài liệu mà một biểu thức cho kết quả là TRUE
|
COUNT_DISTINCT
|
Trả về số lượng giá trị riêng biệt, không phải NULL
|
SUM
|
Trả về tổng của tất cả giá trị NUMERIC
|
AVERAGE
|
Trả về giá trị trung bình của tất cả các giá trị NUMERIC
|
MINIMUM
|
Trả về giá trị không phải NULL nhỏ nhất
|
MAXIMUM
|
Trả về giá trị không phải NULL lớn nhất
|
FIRST
|
Trả về giá trị expression cho tài liệu đầu tiên.
|
LAST
|
Trả về giá trị expression cho tài liệu gần đây nhất.
|
ARRAY_AGG
|
Trả về một mảng gồm tất cả giá trị đầu vào. |
ARRAY_AGG_DISTINCT
|
Trả về một mảng gồm tất cả các giá trị đầu vào riêng biệt. |
COUNT
Cú pháp:
count() -> INT64
count(expression: ANY) -> INT64
Nội dung mô tả:
Trả về số lượng tài liệu từ giai đoạn trước, trong đó expression
đánh giá thành bất kỳ giá trị nào không phải NULL. Nếu không có expression nào được cung cấp, thì hàm này sẽ trả về tổng số tài liệu từ giai đoạn trước.
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").count().as("count")) .execute();
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() )
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("count")) .execute() .get();
Bắt đầu
// Total number of books in the collection countAll, err := client.Pipeline().Collection("books"). Aggregate(firestore.Accumulators(firestore.CountAll().As("count"))). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err } // Number of books with nonnull `ratings` field countField, err := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators(firestore.Count("ratings").As("count"))). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err }
COUNT_IF
Cú pháp:
count_if(expression: BOOLEAN) -> INT64
Nội dung mô tả:
Trả về số lượng tài liệu từ giai đoạn trước, trong đó expression
được đánh giá là TRUE.
Node.js
const result = await db.pipeline() .collection("books") .aggregate( field("rating").greaterThan(4).countIf().as("filteredCount") ) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countIf(field("rating").greaterThan(4)).as("filteredCount")) .execute() .get();
Bắt đầu
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.CountIf(firestore.FieldOf("rating").GreaterThan(4)).As("filteredCount"), )). Execute(ctx)
COUNT_DISTINCT
Cú pháp:
count_distinct(expression: ANY) -> INT64
Nội dung mô tả:
Trả về số lượng giá trị riêng biệt không phải NULL, không phải ABSENT của expression.
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("author").countDistinct().as("unique_authors")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countDistinct("author").as("unique_authors")) .execute() .get();
Bắt đầu
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.CountDistinct("author").As("unique_authors"), )). Execute(ctx)
SUM
Cú pháp:
sum(expression: ANY) -> NUMBER
Nội dung mô tả:
Trả về tổng của tất cả các giá trị số, bỏ qua các giá trị không phải số. Trả về NaN nếu có giá trị là NaN.
Kết quả sẽ có cùng loại với loại dữ liệu đầu vào rộng nhất, ngoại trừ những trường hợp sau:
INTEGERsẽ được chuyển đổi thànhDOUBLEnếu không thể biểu thị dưới dạngINTEGER.
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").sum().as("totalPopulation")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(sum("population").as("totalPopulation")) .execute() .get();
Bắt đầu
snapshot := client.Pipeline(). Collection("cities"). Aggregate(firestore.Accumulators( firestore.Sum("population").As("totalPopulation"), )). Execute(ctx)
AVERAGE
Cú pháp:
average(expression: ANY) -> FLOAT64
Nội dung mô tả:
Trả về giá trị trung bình cho tất cả các giá trị dạng số, bỏ qua các giá trị không phải dạng số.
Đánh giá thành NaN nếu có bất kỳ giá trị nào là NaN hoặc NULL nếu không có giá trị số nào được tổng hợp.
Kết quả sẽ có cùng loại với loại đầu vào, ngoại trừ các trường hợp sau:
INTEGERsẽ được chuyển đổi thànhDOUBLEnếu không thể biểu thị dưới dạngINTEGER.
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").average().as("averagePopulation")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(average("population").as("averagePopulation")) .execute() .get();
Bắt đầu
snapshot := client.Pipeline(). Collection("cities"). Aggregate(firestore.Accumulators( firestore.Average("population").As("averagePopulation"), )). Execute(ctx)
TỐI THIỂU
Cú pháp:
minimum(expression: ANY) -> ANY
Nội dung mô tả:
Trả về giá trị nhỏ nhất không phải NULL, không phải giá trị vắng mặt của expression khi được đánh giá trên mỗi tài liệu.
Nếu không có giá trị nào khác NULL và không có giá trị nào bị thiếu, thì hàm sẽ trả về NULL. Điều này bao gồm cả trường hợp không có tài liệu nào được xem xét.
Nếu có nhiều giá trị tương đương tối thiểu, thì hàm có thể trả về bất kỳ giá trị nào trong số đó. Thứ tự loại giá trị tuân theo thứ tự được ghi nhận.
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("price").minimum().as("minimumPrice")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(minimum("price").as("minimumPrice")) .execute() .get();
Bắt đầu
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.Minimum("price").As("minimumPrice"), )). Execute(ctx)
TỐI ĐA
Cú pháp:
maximum(expression: ANY) -> ANY
Nội dung mô tả:
Trả về giá trị tối đa không phải NULL, không phải giá trị vắng mặt của expression khi được đánh giá trên mỗi tài liệu.
Nếu không có giá trị nào khác NULL và không có giá trị nào bị thiếu, thì hàm sẽ trả về NULL. Điều này bao gồm cả trường hợp không có tài liệu nào được xem xét.
Nếu có nhiều giá trị tương đương tối đa, thì hàm có thể trả về bất kỳ giá trị nào trong số đó. Thứ tự loại giá trị tuân theo thứ tự được ghi nhận.
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("price").maximum().as("maximumPrice")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(maximum("price").as("maximumPrice")) .execute() .get();
Bắt đầu
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.Maximum("price").As("maximumPrice"), )). Execute(ctx)
ĐẦU TIÊN
Cú pháp:
first(expression: ANY) -> ANY
Nội dung mô tả:
Trả về giá trị của expression cho tài liệu được trả về đầu tiên.
CUỐI CÙNG
Cú pháp:
last(expression: ANY) -> ANY
Nội dung mô tả:
Trả về giá trị của expression cho tài liệu được trả về gần đây nhất.
ARRAY_AGG
Cú pháp:
array_agg(expression: ANY) -> ARRAY<ANY>
Nội dung mô tả:
Trả về một mảng chứa tất cả giá trị của expression khi được đánh giá trên mỗi tài liệu.
Nếu biểu thức phân giải thành một giá trị vắng mặt, thì biểu thức đó sẽ được chuyển đổi thành NULL.
Thứ tự của các phần tử trong mảng đầu ra không ổn định và không nên dựa vào đó.
ARRAY_AGG_DISTINCT
Cú pháp:
array_agg_distinct(expression: ANY) -> ARRAY<ANY>
Nội dung mô tả:
Trả về một mảng chứa tất cả các giá trị riêng biệt của expression khi được đánh giá trên mỗi tài liệu.
Nếu biểu thức phân giải thành một giá trị vắng mặt, thì biểu thức đó sẽ được chuyển đổi thành NULL.
Thứ tự của các phần tử trong mảng đầu ra không ổn định và không nên dựa vào đó.