Cloud Firestore מספק פונקציונליות שאילתה רבת עוצמה לציון המסמכים שברצונך לאחזר מאוסף. ניתן להשתמש בשאילתות אלה גם עם get()
או addSnapshotListener()
, כמתואר ב- Get Data .
סדר והגבלת נתונים
כברירת מחדל, שאילתה מאחזרת את כל המסמכים העונים על השאילתה בסדר עולה לפי מזהה מסמך. אתה יכול לציין את סדר המיון עבור הנתונים שלך באמצעות orderBy()
, ותוכל להגביל את מספר המסמכים שאוחזרו באמצעות limit()
.
לדוגמה, תוכל לבצע שאילתות עבור 3 הערים הראשונות בסדר אלפביתי באמצעות:
Web version 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web version 8
citiesRef.orderBy("name").limit(3);
מָהִיר
citiesRef.order(by: "name").limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Kotlin+KTX
citiesRef.orderBy("name").limit(3)
Java
citiesRef.orderBy("name").limit(3);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
Java
פִּיתוֹן
Python
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
ללכת
PHP
PHP
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
אַחְדוּת
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
אוֹדֶם
תוכל גם למיין בסדר יורד כדי לקבל את 3 הערים האחרונות :
Web version 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web version 8
citiesRef.orderBy("name", "desc").limit(3);
מָהִיר
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
Kotlin+KTX
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
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
ללכת
PHP
PHP
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
אַחְדוּת
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
אוֹדֶם
ניתן גם להזמין לפי מספר שדות. לדוגמה, אם רצית לסדר לפי מדינה, ובתוך כל מדינה סדר לפי אוכלוסייה בסדר יורד:
Web version 9
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web version 8
citiesRef.orderBy("state").orderBy("population", "desc");
מָהִיר
citiesRef .order(by: "state") .order(by: "population", descending: true)
Objective-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
Kotlin+KTX
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
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
ללכת
PHP
PHP
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
אַחְדוּת
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
אוֹדֶם
אתה יכול לשלב מסנני where()
עם orderBy()
ו- limit()
. בדוגמה הבאה, השאילתות מגדירות סף אוכלוסייה, ממיינות לפי אוכלוסייה בסדר עולה ומחזירות רק את התוצאות הראשונות החורגות מהסף:
Web version 9
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web version 8
citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
מָהִיר
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
Objective-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
Kotlin+KTX
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
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
ללכת
PHP
PHP
למידע נוסף על התקנה ויצירה של לקוח Cloud Firestore, עיין בספריות לקוח Cloud Firestore .
אַחְדוּת
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
C#
אוֹדֶם
עם זאת, אם יש לך מסנן עם השוואת טווח ( <
, <=
, >
, >=
), ההזמנה הראשונה שלך חייבת להיות באותו שדה, עיין ברשימת מגבלות orderBy()
למטה.
מגבלות
שים לב להגבלות הבאות עבור פסקאות orderBy()
:
- פסקת
orderBy()
מסננת גם את קיומם של השדות הנתונים. ערכת התוצאות לא תכלול מסמכים שאינם מכילים את השדות הנתונים. אם אתה כולל מסנן עם השוואת טווח (
<
,<=
,>
,>=
), ההזמנה הראשונה שלך חייבת להיות באותו שדה:תקף : מסנן טווח ו-
orderBy
באותו שדהWeb version 9
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web version 8
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 version 9
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web version 8
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#
אוֹדֶם
- אינך יכול לסדר את השאילתה שלך לפי כל שדה הכלול בשוויון (
=
) אוin
.