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

एग्रीगेट

सभी एग्रीगेट फ़ंक्शन को 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

जानकारी:

यह फ़ंक्शन, 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();
जाएं
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.CountDistinct("author").As("unique_authors"),
	)).
	Execute(ctx)

SUM

सिंटैक्स:

sum(expression: ANY) -> NUMBER

जानकारी:

यह फ़ंक्शन, बिना संख्या वाली वैल्यू को छोड़कर, अंकों वाली सभी वैल्यू का योग दिखाता है. अगर कोई भी वैल्यू 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();
जाएं
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Sum("population").As("totalPopulation"),
	)).
	Execute(ctx)

औसत

सिंटैक्स:

average(expression: ANY) -> FLOAT64

जानकारी:

यह फ़ंक्शन, बिना संख्या वाली वैल्यू को छोड़कर, सभी संख्या वाली वैल्यू का औसत दिखाता है. अगर कोई भी वैल्यू 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();
जाएं
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Average("population").As("averagePopulation"),
	)).
	Execute(ctx)

MINIMUM

सिंटैक्स:

minimum(expression: ANY) -> ANY

जानकारी:

हर दस्तावेज़ का आकलन करने पर, यह फ़ंक्शन expression की ऐसी सबसे छोटी वैल्यू दिखाता है जो न तो NULL हो और न ही मौजूद न हो.

अगर कोई भी वैल्यू 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

सिंटैक्स:

maximum(expression: ANY) -> ANY

जानकारी:

हर दस्तावेज़ का आकलन करने पर, expression की ज़्यादा से ज़्यादा वैल्यू दिखाता है. यह वैल्यू, NULL या मौजूद नहीं है के तौर पर नहीं होती.

अगर कोई भी वैल्यू 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

सिंटैक्स:

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 में बदल दिया जाता है.

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