תיאור
מיון מסמכי הקלט לפי סדר מיון אחד או יותר.
דוגמאות
Web
const results = await execute(db.pipeline() .collection("books") .sort( field("release_date").descending(), field("author").ascending() ) );
Swift
let results = try await db.pipeline() .collection("books") .sort([ Field("release_date").descending(), Field("author").ascending() ]) .execute()
Kotlin
val results = db.pipeline() .collection("books") .sort( field("release_date").descending(), field("author").ascending() ) .execute()
Java
Task<Pipeline.Snapshot> results = db.pipeline() .collection("books") .sort( field("release_date").descending(), field("author").ascending() ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field results = ( client.pipeline() .collection("books") .sort(Field.of("release_date").descending(), Field.of("author").ascending()) .execute() )
Java
Pipeline.Snapshot results = firestore .pipeline() .collection("books") .sort(descending(field("release_date")), ascending(field("author"))) .execute() .get();
המשך
snapshot := client.Pipeline(). Collection("books"). Sort(firestore.Orders( firestore.Descending(firestore.FieldOf("release_date")), firestore.Ascending(firestore.FieldOf("author")), )). Execute(ctx)
התנהגות
סדר מיון
סדר המיון נקבע לפי סדר סוגי הערכים של Cloud Firestore
סדר דטרמיניסטי של תוצאות
אם אין שלב sort בשאילתה, סדר התוצאות שמוחזרות הוא לא דטרמיניסטי ויכול להשתנות בין הרצות. אם יש שלב sort אבל הביטויים של הסדר לא מצליחים ליצור סדר ייחודי בין התוצאות שמוחזרות, יכול להיות שהסדר של התוצאות שמוחזרות עדיין ישתנה בין ההפעלות.
לדוגמה, מיון ערים לפי country של הערים, בסדר עולה, מול קבוצת הנתונים הבאה:
{ name: "Los Angeles", state: "CA", country: "USA", population: 3970000 },
{ name: "New York", state: "NY", country: "USA", population: 8530000 }
{ name: "San Francisco", state: "CA", country: "USA", population: 870000 },
יכולה להפיק כל תמורה של 3 המסמכים בקבוצת הנתונים כי לכולם יש את אותו מאפיין country. כדי ליצור סדר דטרמיניסטי של התוצאות, אפשר להוסיף סדר מיון ב-name לסדר:
Node.js
const results = await db.pipeline()
.collection("/cities")
.sort(field("country").ascending(), fieldd("__name__").ascending())
.execute();
כך, שם המסמך הייחודי ישמש כשובר שוויון כשבכמה מסמכים יש את אותו ערך country. שימו לב שאפשר להשתמש בכל שדה אחר שיחד עם השדה country יוצר מפתח ייחודי של המסמך באוסף, כדי ליצור סדר דטרמיניסטי של תוצאות.
מיון ערכים שווי ערך
ערכים שווים ממוינים ביחד, אבל סדר התוצאות בתוך המחלקה השווה לא דטרמיניסטי, בהתאם לדיון על סדר דטרמיניסטי של תוצאות. לדוגמה, מיון ערים לפי size בסדר עולה בקבוצת הנתונים הבאה:
{ name: "Los Angeles", state: "CA", country: "USA", size: 3970000 },
{ name: "Mexico City", state: null, country: "Mexico", size: 3970000.0 },
יכולה להפיק כל תמורה של 2 המסמכים במערך הנתונים, כי לשני המסמכים יש ערך size שווה ערך ל-3970000.
שלבי מיון מרובים
כשהשאילתה מכילה כמה שלבי מיון עוקבים, רק לשלב המיון האחרון יש השפעה על תוצאות השאילתה. שימו לב שההתנהגות הזו שונה מההתנהגות של הסעיף orderBy ב-Core API.
אופטימיזציה של מיון Top-N
כשמשתמשים ב-limit(...) אחרי sort, יכול להיות שיתבצע מיון של n הפריטים הראשונים. האופטימיזציה הזו מגבילה את השימוש בזיכרון של שלב המיון, בכך שהיא מאפשרת לו לאחסן רק N מסמכים בכל פעם – כפי שמוגדר על ידי limit(...) – וכך הופכת את המיון ליעיל יותר מבחינת הזיכרון.
ערכים ריקים וחסרים
אם שדה שצוין בסדר מסוים לא קיים במסמך, הערך שלו ממוין כאילו הוא null. לדוגמה, מיון ערים לפי state של הערים, בסדר עולה, לפי קבוצת הנתונים הבאה:
{ name: "Los Angeles", state: "CA", country: "USA", population: 3970000 },
{ name: "Mexico City", state: null, country: "Mexico", population: 9200000 },
{ name: "New York", state: "NY", country: "USA", population: 8530000 }
{ name: "San Francisco", state: "CA", country: "USA", population: 870000 },
{ name: "Toronto", country: "Canada", population: 2930000 },
מפיקה את התוצאות הבאות, שבהן המסמך "Toronto" והמסמך "Mexico City" ממוינים כ-null ולפני מסמכים אחרים.
{ name: "Toronto", country: "Canada", population: 2930000 },
{ name: "Mexico City", state: null, country: "Mexico", population: 9200000 },
{ name: "Los Angeles", state: "CA", country: "USA", population: 3970000 },
{ name: "San Francisco", state: "CA", country: "USA", population: 870000 },
{ name: "New York", state: "NY", country: "USA", population: 8530000 }