Cloud Firestore מספק פונקציונליות שאילתה רבת עוצמה לציון המסמכים שברצונך לאחזר מאוסף. ניתן להשתמש בשאילתות אלה גם עם get()
או addSnapshotListener()
, כמתואר ב- Get Data .
כברירת מחדל, שאילתה מאחזרת את כל המסמכים העונים על השאילתה בסדר עולה לפי מזהה מסמך. אתה יכול לציין את סדר המיון עבור הנתונים שלך באמצעות orderBy()
, ותוכל להגביל את מספר המסמכים שאוחזרו באמצעות limit()
. אם תציין limit()
, הערך חייב להיות גדול או שווה לאפס.
לדוגמה, תוכל לבצע שאילתות עבור 3 הערים הראשונות בסדר אלפביתי באמצעות:
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
citiesRef.orderBy("name").limit(3);
citiesRef.order(by: "name").limit(to: 3)
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
citiesRef.orderBy("name").limit(3)
citiesRef.orderBy("name").limit(3);
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
cities_ref.OrderBy("name").Limit(3);
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
Query query = citiesRef.OrderBy("Name").Limit(3);
תוכל גם למיין בסדר יורד כדי לקבל את 3 הערים האחרונות :
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
citiesRef.orderBy("name", "desc").limit(3);
citiesRef.order(by: "name", descending: true).limit(to: 3)
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descending: true).limit(3);
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
Query query = citiesRef.OrderByDescending("Name").Limit(3);
ניתן גם להזמין לפי מספר שדות. לדוגמה, אם רצית לסדר לפי מדינה, ובתוך כל מדינה סדר לפי אוכלוסייה בסדר יורד:
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
citiesRef.orderBy("state").orderBy("population", "desc");
citiesRef .order(by: "state") .order(by: "population", descending: true)
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("population", descending: true);
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
אתה יכול לשלב מסנני where()
עם orderBy()
ו- limit()
. בדוגמה הבאה, השאילתות מגדירות סף אוכלוסייה, ממיינות לפי אוכלוסייה בסדר עולה ומחזירות רק את התוצאות הראשונות החורגות מהסף:
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("population") .limit(2);
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
עם זאת, אם יש לך מסנן עם השוואת טווח ( <
, <=
, >
, >=
), ההזמנה הראשונה שלך חייבת להיות באותו שדה, עיין ברשימת מגבלות orderBy()
למטה.
שים לב להגבלות הבאות עבור פסקאות orderBy()
:
- פסקת
orderBy()
מסננת גם את קיומם של השדות הנתונים . ערכת התוצאות לא תכלול מסמכים שאינם מכילים את השדות הנתונים. אם אתה כולל מסנן עם השוואת טווח (
<
,<=
,>
,>=
), ההזמנה הראשונה שלך חייבת להיות באותו שדה:תקף : מסנן טווח ו-
orderBy
באותו שדהWeb modular API import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web namespaced API citiesRef.where("population", ">", 100000).orderBy("population");
מָהִיר הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
Objective-C הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"];
Kotlin+KTX citiesRef.whereGreaterThan("population", 100000).orderBy("population")
Java citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Dart final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
Java Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
פִּיתוֹן Python Node.js ללכת PHP PHP למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
אַחְדוּת Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
C# אוֹדֶם לא חוקי : מסנן טווח
orderBy
ראשון לפי בשדות שוניםWeb modular API import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web namespaced API citiesRef.where("population", ">", 100000).orderBy("country");
מָהִיר הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
Objective-C הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
Kotlin+KTX citiesRef.whereGreaterThan("population", 100000).orderBy("country")
Java citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Dart final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
Java פִּיתוֹן Python C++ // BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
Node.js ללכת PHP PHP למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
אַחְדוּת Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
C# אוֹדֶם
orderBy
וקיוםכאשר אתה מזמין שאילתה לפי שדה נתון, השאילתה יכולה להחזיר רק את המסמכים שבהם קיים השדה לפי סדר.
לדוגמה, השאילתה הבאה לא תחזיר מסמכים שבהם שדה population
אינו מוגדר, גם אם הם עומדים במסנני השאילתה אחרת.
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
השפעה קשורה חלה על אי שוויון. שאילתה עם מסנן אי-שוויון בשדה מרמזת גם על סדר לפי שדה זה. השאילתה הבאה אינה מחזירה מסמכים ללא שדה population
גם אם country = USA
במסמך זה. כפתרון עוקף, אתה יכול לבצע שאילתות נפרדות עבור כל הזמנה או שאתה יכול להקצות ערך לכל השדות לפיהם אתה מזמין.
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
השאילתה שלמעלה כוללת סדר מרומז על אי השוויון והיא שווה ערך לדברים הבאים:
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);