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

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

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();

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();

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();

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();

औसत

सिंटैक्स:

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();

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();

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();

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

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