एग्रीगेट फ़ंक्शन

Aggregate

aggregate(...) स्टेज में, सभी एग्रीगेट फ़ंक्शन को टॉप-लेवल एक्सप्रेशन के तौर पर इस्तेमाल किया जा सकता है.

नाम Description
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

Description:

पिछले स्टेज के उन दस्तावेज़ों की संख्या दिखाता है जिनमें 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();
Go
// 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

Description:

पिछले स्टेज के उन दस्तावेज़ों की संख्या दिखाता है जिनमें 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();
Go
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

Description:

expression की यूनीक नॉन-NULL, नॉन-ABSENT वैल्यू की संख्या दिखाता है.

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();
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.CountDistinct("author").As("unique_authors"),
	)).
	Execute(ctx)

SUM

सिंटैक्स:

sum(expression: ANY) -> NUMBER

Description:

नॉन-न्यूमेरिक वैल्यू को छोड़कर, सभी न्यूमेरिक वैल्यू का योग दिखाता है. अगर कोई वैल्यू NaN है, तो NaN दिखाता है.

आउटपुट का टाइप, सबसे बड़े इनपुट टाइप जैसा होगा. हालांकि, इन मामलों में ऐसा नहीं होगा:

  • अगर INTEGER को INTEGER के तौर पर नहीं दिखाया जा सकता, तो उसे DOUBLE में बदल दिया जाएगा.
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();
Go
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Sum("population").As("totalPopulation"),
	)).
	Execute(ctx)

AVERAGE

सिंटैक्स:

average(expression: ANY) -> FLOAT64

Description:

नॉन-न्यूमेरिक वैल्यू को छोड़कर, सभी न्यूमेरिक वैल्यू का औसत दिखाता है. अगर कोई वैल्यू NaN है, तो NaN दिखाता है. अगर कोई न्यूमेरिक वैल्यू एग्रीगेट नहीं की जाती है, तो NULL दिखाता है.

आउटपुट का टाइप, इनपुट टाइप जैसा होगा. हालांकि, इन मामलों में ऐसा नहीं होगा:

  • अगर INTEGER को INTEGER के तौर पर नहीं दिखाया जा सकता, तो उसे DOUBLE में बदल दिया जाएगा.
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();
Go
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Average("population").As("averagePopulation"),
	)).
	Execute(ctx)

MINIMUM

सिंटैक्स:

minimum(expression: ANY) -> ANY

Description:

हर दस्तावेज़ पर आकलन करने के बाद, expression की कम से कम नॉन-NULL, नॉन-absent वैल्यू दिखाता है.

अगर कोई नॉन-NULL, नॉन-absent वैल्यू नहीं है, तो 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();
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.Minimum("price").As("minimumPrice"),
	)).
	Execute(ctx)

MAXIMUM

सिंटैक्स:

maximum(expression: ANY) -> ANY

Description:

हर दस्तावेज़ पर आकलन करने के बाद, expression की ज़्यादा से ज़्यादा नॉन-NULL, नॉन-absent वैल्यू दिखाता है.

अगर कोई नॉन-NULL, नॉन-absent वैल्यू नहीं है, तो 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();
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.Maximum("price").As("maximumPrice"),
	)).
	Execute(ctx)

FIRST

सिंटैक्स:

first(expression: ANY) -> ANY

Description:

दस्तावेज़ के लिए, expression की वैल्यू दिखाता है.

LAST

सिंटैक्स:

last(expression: ANY) -> ANY

Description:

दस्तावेज़ के लिए, expression की वैल्यू दिखाता है.

ARRAY_AGG

सिंटैक्स:

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

Description:

हर दस्तावेज़ पर आकलन करने के बाद, expression की सभी वैल्यू वाला ऐरे दिखाता है.

अगर एक्सप्रेशन की वैल्यू absent है, तो उसे NULL में बदल दिया जाता है.

आउटपुट ऐरे में एलिमेंट का क्रम स्थिर नहीं होता. इसलिए, इस पर भरोसा नहीं किया जाना चाहिए.

ARRAY_AGG_DISTINCT

सिंटैक्स:

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

Description:

हर दस्तावेज़ पर आकलन करने के बाद, expression की सभी अलग-अलग वैल्यू वाला ऐरे दिखाता है.

अगर एक्सप्रेशन की वैल्यू absent है, तो उसे NULL में बदल दिया जाता है.

आउटपुट ऐरे में एलिमेंट का क्रम स्थिर नहीं होता. इसलिए, इस पर भरोसा नहीं किया जाना चाहिए.