चुनें

ब्यौरा

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

उदाहरण

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();
जाएं
snapshot := client.Pipeline().
	Collection("books").
	Select(firestore.Fields(
		firestore.Round(firestore.Multiply(firestore.FieldOf("soldBooks"), firestore.FieldOf("price"))).As("partialRevenue"),
	)).
	Aggregate(firestore.Accumulators(
		firestore.Sum("partialRevenue").As("totalRevenue"),
	)).
	Execute(ctx)

व्यवहार

दस्तावेज़ के मेटाडेटा फ़ील्ड

select(...) स्टेज सिर्फ़ उन फ़ील्ड या एक्सप्रेशन को दिखाता है जिन्हें स्टेज में साफ़ तौर पर बताया गया है. यह कोर ऑपरेशन्स से अलग है. इसमें क्वेरी पर select(...) सेटिंग, प्रोजेक्शन के बजाय प्रॉपर्टी मास्क के तौर पर काम करती है. साथ ही, इसमें __name__, __create_time__, और __update_time__ के दस्तावेज़ के मेटाडेटा फ़ील्ड हमेशा शामिल होते हैं.

select(...) में कोर ऑपरेशंस जैसा व्यवहार पाने के लिए, फ़ील्ड को साफ़ तौर पर इस तरह से तय किया जाना चाहिए:

Node.js

const results = await db.pipeline()
  .collection("/users")
  .select(
    field("__name__"),
    field("__create_time__"),
    field("__update_time__"),
    field("email"))
  .execute();

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