الدوالّ المجمّعة

تجميع

يمكن استخدام جميع دوال التجميع كتعبيرات ذات مستوى أعلى في مرحلة 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").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();
متابعة
// 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

البنية:

count_if(expression: BOOLEAN) -> INT64

الوصف:

تعرض هذه الدالة عدد المستندات من المرحلة السابقة التي يكون فيها تقييم expression هو 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();
متابعة
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.CountIf(firestore.FieldOf("rating").GreaterThan(4)).As("filteredCount"),
	)).
	Execute(ctx)

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_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();
متابعة
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.CountDistinct("author").As("unique_authors"),
	)).
	Execute(ctx)

SUM

البنية:

sum(expression: ANY) -> NUMBER

الوصف:

تعرض مجموع كل القيم الرقمية، مع تجاهل القيم غير الرقمية. يتم عرض NaN إذا كانت أي قيم NaN.

سيكون الناتج من النوع نفسه لأوسع نوع إدخال باستثناء الحالات التالية:

  • سيتم تحويل INTEGER إلى DOUBLE إذا تعذّر تمثيله كـ INTEGER.
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();
متابعة
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Sum("population").As("totalPopulation"),
	)).
	Execute(ctx)

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("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();
متابعة
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Average("population").As("averagePopulation"),
	)).
	Execute(ctx)

الحد الأدنى

البنية:

minimum(expression: ANY) -> ANY

الوصف:

تعرض الحد الأدنى للقيمة غير الفارغة وغير NULL لـ expression عند تقييمها في كل مستند.

إذا لم تكن هناك قيم غير NULL وغير فارغة، يتم عرض NULL. ويشمل ذلك الحالات التي لا يتم فيها النظر في أي مستندات.

إذا كانت هناك قيم دنيا متكافئة متعددة، يمكن عرض أي من هذه القيم. يتبع ترتيب نوع القيمة الترتيب الموثَّق.

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();
متابعة
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.Minimum("price").As("minimumPrice"),
	)).
	Execute(ctx)

الحد الأقصى

البنية:

maximum(expression: ANY) -> ANY

الوصف:

تعرض هذه الدالة الحد الأقصى للقيمة غير الفارغة وغير NULL لـ expression عند تقييمها في كل مستند.

إذا لم تكن هناك قيم غير NULL وغير فارغة، يتم عرض NULL. ويشمل ذلك الحالات التي لا يتم فيها أخذ أي مستندات في الاعتبار.

في حال توفّر قيم قصوى متكافئة متعددة، يمكن عرض أيّ منها. يتبع ترتيب نوع القيمة الترتيب الموثَّق.

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();
متابعة
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.Maximum("price").As("maximumPrice"),
	)).
	Execute(ctx)

أولاً

البنية:

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.

ترتيب العناصر في مصفوفة الإخراج غير ثابت ولا يجب الاعتماد عليه.