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

Aggregate

सभी एग्रीगेट फ़ंक्शन का इस्तेमाल, 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 नहीं दिया जाता है, तो यह पिछले चरण के दस्तावेज़ों की कुल संख्या दिखाता है.

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

ब्यौरा:

यह फ़ंक्शन, expression की उन यूनीक वैल्यू की संख्या दिखाता है जो NULL और ABSENT नहीं हैं.

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 के तौर पर नहीं दिखाया जा सकता, तो उसे 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(expression: ANY) -> FLOAT64

ब्यौरा:

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

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

  • अगर किसी INTEGER को DOUBLE के तौर पर नहीं दिखाया जा सकता, तो उसे 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

सिंटैक्स:

minimum(expression: ANY) -> ANY

ब्यौरा:

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

अगर 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

सिंटैक्स:

maximum(expression: ANY) -> ANY

ब्यौरा:

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

अगर 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

सिंटैक्स:

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

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