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