תיאור
מציאת כל השילובים הייחודיים של ערכים בסדרה של ביטויים.
התחביר של השלב 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 עם מסמכים שונים שמכילים ערכי foo של מספר שלם של 32 ביט 1, מספר שלם של 64 ביט 1L ונקודה צפה (floating point) 1.0 בהתאמה, distinct(...) יחזיר רק תוצאה אחת.
במקרים כאלה, שבהם יש ערכים שווי ערך שונים בקבוצת הנתונים, ערך הפלט של הקבוצה יכול להיות כל אחד מהערכים שווי הערך האלה.
בדוגמה הזו, הערך של foo יכול להיות 1, 1L או 1.0.
גם אם נראה שההתנהגות היא דטרמיניסטית, אל תנסו להסתמך על ההתנהגות של בחירת ערך ספציפי אחד.
שימוש בזיכרון
אופן הביצוע של השלב distinct(...) תלוי באינדקסים הזמינים. אם אין אינדקס מתאים שנבחר על ידי כלי האופטימיזציה של השאילתות, distinct(...) צריך לשמור במאגר נתונים זמני את כל הערכים הייחודיים בזיכרון.
אם יש מספר גדול מאוד של ערכים ייחודיים, או אם הערכים גדולים מאוד (למשל, ערכים ייחודיים גדולים מאוד), יכול להיות שייגמר הזיכרון בשלב הזה.
במקרים כאלה, כדאי להחיל מסננים כדי להגביל את מערך הנתונים שעליו מפעילים את distinct(...), או ליצור אינדקסים לפי ההמלצות כדי להימנע משימוש נרחב בזיכרון.
ההסבר על השאילתה יספק מידע על תוכנית הביצוע בפועל של השאילתה ועל נתוני הפרופיילינג כדי לעזור בניפוי הבאגים.