चुनें

ब्यौरा

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

उदाहरण

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue"))
  .aggregate(field("partialRevenue").sum().as("totalRevenue"))
  );
Swift
let result = try await db.pipeline()
  .collection("books")
  .select([Field("soldBooks").multiply(Field("price")).round().as("partialRevenue")])
  .aggregate([Field("partialRevenue").sum().as("totalRevenue")])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .select(Expression.multiply(field("soldBooks"), field("price")).round().alias("partialRevenue"))
    .aggregate(AggregateFunction.sum("partialRevenue").alias("totalRevenue"))
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .select(Expression.multiply(field("soldBooks"), field("price")).round().alias("partialRevenue"))
    .aggregate(AggregateFunction.sum("partialRevenue").alias("totalRevenue"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .select(
        Field.of("soldBooks")
        .multiply(Field.of("price"))
        .round()
        .as_("partialRevenue")
    )
    .aggregate(Field.of("partialRevenue").sum().as_("totalRevenue"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .select(round(multiply(field("soldBooks"), field("price"))).as("partialRevenue"))
        .aggregate(sum("partialRevenue").as("totalRevenue"))
        .execute()
        .get();

व्यवहार

Select Stage की पोज़िशन

किसी स्टेज को कब इस्तेमाल किया जा सकता है, इस पर कोई पाबंदी नहीं है. हालांकि, किसी पाइपलाइन में बाद के चरणों में, उन फ़ील्ड को ऐक्सेस नहीं किया जा सकेगा जिन्हें किसी स्टेज में शामिल नहीं किया गया है. उदाहरण के लिए, अगर आपको इस डेटासेट से कनाडा के सभी शहरों के सिर्फ़ name और location फ़ील्ड चुनने हैं, तो:

Node.js

await db.collection("cities").doc("SF").set({
  name: "San Francisco",
  population: 800000,
  location: {country: "USA", state: "California"}
});

await db.collection("cities").doc("TO").set({
  name: "Toronto",
  population: 3000000,
  location: {country: "Canada", province: "Ontario"}
});

इस पाइपलाइन का इस्तेमाल किया जा सकता है:

Node.js

const names = await db.pipeline()
  .collection("/cities")
  .where(equal(field("location.country"), "Canada"))
  .select(stringConcat(field("name"), ", ", field("location.country")).as("name"), "population")
  .execute();

इससे ये दस्तावेज़ जनरेट होते हैं:

{ name: "Toronto, Canada", population: 3000000 },

हालांकि, अगर select(...) स्टेज को where(...) स्टेज से पहले रखा जाता है, तो:

Node.js

const names = await db.pipeline()
  .collection("/cities")
  .select(stringConcat(field("name"), ",", field("location.country")).as("name"), "population")
  .where(equal(field("location.country"), "Canada"))
  .execute();

कोई दस्तावेज़ नहीं बनाया जाएगा, क्योंकि where(...) स्टेज को लागू करने से पहले, दस्तावेज़ से location.country को हटा दिया गया है.

नेस्ट किए गए फ़ील्ड चुनना

select(...) स्टेज का इस्तेमाल, मैप और ऐरे, दोनों से नेस्ट किए गए फ़ील्ड चुनने के लिए किया जा सकता है. उदाहरण के लिए, यहां दिए गए दस्तावेज़ों से नेस्ट किए गए country फ़ील्ड और landmarks ऐरे की पहली एंट्री चुनने के लिए:

Node.js

await db.collection("cities").doc("SF").set({
  name: "San Francisco",
  population: 800000,
  location: { country: "USA", state: "California" },
  landmarks: [ "Golden Gate Bridge", "Alcatraz" ]
});

await db.collection("cities").doc("TO").set({
  name: "Toronto",
  population:  3000000,
  province: "ON",
  location: { country: "Canada", province: "Ontario" },
  landmarks: [ "CN Tower", "Casa Loma" ]
});

await db.collection("cities").doc("AT").set({
  name: "Atlantis",
  population: null
});

इस पाइपलाइन का इस्तेमाल किया जा सकता है:

Node.js

const locations = await db.pipeline()
  .collection("/cities")
  .select(
    field("name").as("city"),
    field("location.country").as("country"),
    field("landmarks").offset(0).as("topLandmark"))
  .execute();

इससे ये दस्तावेज़ जनरेट होते हैं:

{ city: "San Francisco", country: "USA", topLandmark: "Golden Gate Bridge" },
{ city: "Toronto", country: "Canada", topLandmark: "CN Tower" },
{ city: "Atlantis" }

अगर नेस्ट किए गए मैप की वैल्यू या ऐरे की वैल्यू मौजूद नहीं है, तो उसे नतीजे के तौर पर मिले दस्तावेज़ में शामिल नहीं किया जाता. चुने गए चरण में, ऐरे और मैप ऐक्सेस करने का तरीका, offset(...) और get_field(...) फ़ंक्शन के तरीके जैसा ही होता है.

नेस्ट किए गए फ़ील्ड असाइन करना

किसी एक्सप्रेशन के नतीजे को नेस्ट किए गए फ़ील्ड को भी असाइन किया जा सकता है. इससे select(...) स्टेज, पिछली स्टेज से नेस्ट किए गए फ़ील्ड का सबसेट दिखा सकती है. उदाहरण के लिए, यहां दिए गए कोड का इस्तेमाल करके यह पक्का किया जा सकता है कि सिर्फ़ city और state की जानकारी मिले. साथ ही, दस्तावेज़ का ओरिजनल फ़ॉर्मैट भी बना रहे:

Node.js

const results = await db.pipeline()
  .collection("/users")
  .addFields(
    field("__name__"),
    field("address.city").as("address.city"),
    field("address.state").as("address.state"))
  .execute();