ब्यौरा
एक्सप्रेशन की सीरीज़ के लिए, वैल्यू के सभी अलग-अलग कॉम्बिनेशन ढूंढना.
distinct(...) स्टेज का सिंटैक्स, select(...) जैसा ही होता है. ऐसा इसलिए, क्योंकि इसमें एक या उससे ज़्यादा एक्सप्रेशन चुने जा सकते हैं. स्ट्रिंग का इस्तेमाल तब किया जा सकता है, जब एक्सप्रेशन सिर्फ़ एक फ़ील्ड रेफ़रंस हो:
उदाहरण
Node.js
let cities = await db.pipeline() .collection("cities") .distinct("country") .execute(); cities = await db.pipeline() .collection("cities") .distinct( field("state").toLower().as("normalizedState"), field("country")) .execute();
Web
let cities = await execute(db.pipeline() .collection("cities") .distinct("country")); cities = await execute(db.pipeline() .collection("cities") .distinct( field("state").toLower().as("normalizedState"), field("country")));
Swift
let results = try await db.pipeline() .collection("books") .distinct([ Field("author").toUpper().as("author"), Field("genre") ]) .execute()
Kotlin
var cities = db.pipeline() .collection("cities") .distinct("country") .execute() cities = db.pipeline() .collection("cities") .distinct( field("state").toLower().alias("normalizedState"), field("country") ) .execute()
Java
Task<Pipeline.Snapshot> cities; cities = db.pipeline() .collection("cities") .distinct("country") .execute(); cities = db.pipeline() .collection("cities") .distinct( field("state").toLower().alias("normalizedState"), field("country")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field cities = client.pipeline().collection("cities").distinct("country").execute() cities = ( client.pipeline() .collection("cities") .distinct(Field.of("state").to_lower().as_("normalizedState"), "country") .execute() )
Java
Pipeline.Snapshot cities1 = firestore.pipeline().collection("cities").distinct("country").execute().get(); Pipeline.Snapshot cities2 = firestore .pipeline() .collection("cities") .distinct(toLower(field("state")).as("normalizedState"), field("country")) .execute() .get();
जाएं
cities1, err := client.Pipeline().Collection("cities").Distinct(firestore.Fields("country")).Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err } cities2, err := client.Pipeline().Collection("cities"). Distinct(firestore.Fields( firestore.ToLower(firestore.FieldOf("state")).As("normalizedState"), firestore.FieldOf("country"), )). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err }
व्यवहार
distinct(...) स्टेज, बिना ग्रुप वाले aggregate(...) स्टेज की तरह ही काम करता है. aggregate(...) और select(...) भी देखें.
अलग-अलग फ़ील्ड वैल्यू ढूंढना
उदाहरण के लिए, इस cities कलेक्शन में मौजूद हर देश की सूची पाने के लिए:
Node.js
await db.collection("cities").doc("SF").set({name: "San Francisco", state: "CA", country: "USA"});
await db.collection("cities").doc("LA").set({name: "Los Angeles", state: "CA", country: "USA"});
await db.collection("cities").doc("NY").set({name: "New York", state: "NY", country: "USA"});
await db.collection("cities").doc("TOR").set({name: "Toronto", state: null, country: "Canada"});
await db.collection("cities").doc("MEX").set({name: "Mexico City", state: null, country: "Mexico"});
अलग-अलग देशों की जानकारी पाने के लिए, इनका इस्तेमाल किया जा सकता है:
Node.js
const cities = await db.pipeline()
.collection("/cities")
.distinct("country")
.execute();
इससे यह नतीजा मिलता है:
{ country: "USA" }
{ country: "Canada" }
{ country: "Mexico" }
एक्सप्रेशन का अलग-अलग आउटपुट
कई फ़ील्ड के अलग-अलग कॉम्बिनेशन या ज़्यादा मुश्किल एक्सप्रेशन भी ढूंढे जा सकते हैं. उदाहरण के लिए:
Node.js
const cities = await db.pipeline()
.collection("/cities")
.distinct(
field("state").toLower().as("normalized_state"),
field("country"))
.execute();
पाने के लिए:
{ country: "USA", normalized_state: "ca" }
{ country: "USA", normalized_state: "ny" }
{ country: "Canada", normalized_state: null }
{ country: "Mexico", normalized_state: null }
इक्विवैलेंस बिहेवियर
अलग-अलग वैल्यू पर समानता का व्यवहार, समानता के समान सिमैंटिक का पालन करता है.
इसका मतलब है कि बराबर वैल्यू को एक ही यूनीक वैल्यू माना जाता है. उदाहरण के लिए, गणित के हिसाब से बराबर संख्या वाली वैल्यू. भले ही, ओरिजनल टाइप (32-बिट इंटिजर, 64-बिट इंटिजर, फ़्लोटिंग पॉइंट नंबर, दशमलव संख्याएं वगैरह) कुछ भी हो.
उदाहरण के लिए, किसी कलेक्शन numerics में अलग-अलग दस्तावेज़ हैं. इनमें 32-बिट इंटिजर 1, 64-बिट इंटिजर 1L, और फ़्लोटिंग पॉइंट 1.0 की foo वैल्यू हैं. ऐसे में, distinct(...) सिर्फ़ एक नतीजा दिखाएगा.
डेटासेट में एक जैसी कई वैल्यू मौजूद होने पर, ग्रुप की आउटपुट वैल्यू इनमें से कोई भी हो सकती है.
इस उदाहरण में, foo की यह वैल्यू 1, 1L या 1.0 के तौर पर दिखाई जा सकती है.
भले ही, यह डिटरमिनिस्टिक एल्गोरिदम जैसा लगे, लेकिन आपको किसी एक वैल्यू के चुने जाने के व्यवहार पर भरोसा नहीं करना चाहिए.
मेमोरी का इस्तेमाल
distinct(...) स्टेज को कैसे लागू किया जाता है, यह उपलब्ध इंडेक्स पर निर्भर करता है. जब क्वेरी ऑप्टिमाइज़र सही इंडेक्स नहीं चुनता है, तो distinct(...) को मेमोरी में सभी अलग-अलग वैल्यू को बफ़र करना पड़ता है.
अगर बहुत ज़्यादा संख्या में अलग-अलग वैल्यू हैं या वैल्यू बहुत बड़ी हैं (उदाहरण के लिए, बड़ी वैल्यू पर अलग-अलग वैल्यू), तो इस स्टेज में आउट ऑफ़ मेमोरी हो सकती है.
ऐसे मामलों में, आपको फ़िल्टर लागू करके डेटासेट को सीमित करना चाहिए, ताकि distinct(...) को चलाया जा सके. इसके अलावा, मेमोरी के ज़्यादा इस्तेमाल से बचने के लिए, सुझाई गई इंडेक्स बनाएं.
Query Explain, क्वेरी को लागू करने के असल प्लान और प्रोफ़ाइलिंग डेटा के बारे में जानकारी देगा, ताकि डीबग करने में मदद मिल सके.