הזמנה והגבלה של נתונים באמצעות Cloud Firestore

Cloud Firestore מספק פונקציונליות יעילה של שאילתות כדי לציין את המסמכים שרוצים לאחזר מאוסף. אפשר להשתמש בשאילתות האלה גם עם get() או addSnapshotListener(), כפי שמתואר בקטע קבלת נתונים.

סדרת נתונים והגבלת נתונים

כברירת מחדל, שאילתות מאחזרות את כל המסמכים שעומדים בדרישות השאילתה בסדר עולה לפי מזהה המסמך. אפשר לציין את סדר המיון של הנתונים באמצעות orderBy(), ולהגביל את מספר המסמכים שאוחזרים באמצעות limit(). אם מציינים ערך של limit(), הערך חייב להיות גדול מאפס או שווה לו.

לדוגמה, אפשר להריץ שאילתה ל-3 הערים הראשונות לפי סדר אלפביתי באמצעות:

import { query, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, orderBy("name"), limit(3));
citiesRef.orderBy("name").limit(3);
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
citiesRef.order(by: "name").limit(to: 3)
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
[[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);
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = query.get()
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = await query.get()
cities_ref.OrderBy("name").Limit(3);
const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();
query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestore.Asc).LimitToLast(3)

PHP

מידע נוסף על התקנה ויצירה של לקוח Cloud Firestore זמין במאמר ספריות לקוח של Cloud Firestore.

$query = $citiesRef->orderBy('name')->limit(3);
Query query = citiesRef.OrderBy("Name").Limit(3);
Query query = citiesRef.OrderBy("Name").Limit(3);
query = cities_ref.order("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);
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
citiesRef.order(by: "name", descending: true).limit(to: 3)
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
[[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);
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()
cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
query := cities.OrderBy("name", firestore.Desc).Limit(3)

PHP

מידע נוסף על התקנה ויצירה של לקוח Cloud Firestore זמין במאמר ספריות לקוח של Cloud Firestore.

$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
Query query = citiesRef.OrderByDescending("Name").Limit(3);
Query query = citiesRef.OrderByDescending("Name").Limit(3);
query = cities_ref.order("name", "desc").limit(3)

אפשר גם למיין לפי כמה שדות. לדוגמה, אם רוצים למיין לפי מדינה, ובכל מדינה לפי אוכלוסייה בסדר יורד:

import { query, orderBy } from "firebase/firestore";  

const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
citiesRef.orderBy("state").orderBy("population", "desc");
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
citiesRef
  .order(by: "state")
  .order(by: "population", descending: true)
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
[[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);
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
cities_ref = db.collection("cities")
ordered_city_ref = cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)
cities_ref = db.collection("cities")
cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)

PHP

מידע נוסף על התקנה ויצירה של לקוח Cloud Firestore זמין במאמר ספריות לקוח של Cloud Firestore.

$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
query = cities_ref.order("state").order("population", "desc")

אפשר לשלב מסנני 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);
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
citiesRef
  .whereField("population", isGreaterThan: 100000)
  .order(by: "population")
  .limit(to: 2)
הערה: המוצר הזה לא זמין ליעדים של watchOS ו-App Clip.
[[[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);
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
    .OrderBy("population")
    .Limit(2);
const biggestRes = await citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2).get();
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)

PHP

מידע נוסף על התקנה ויצירה של לקוח Cloud Firestore זמין במאמר ספריות לקוח של Cloud Firestore.

$query = $citiesRef
    ->where('population', '>', 2500000)
    ->orderBy('population')
    ->limit(2);
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
query = cities_ref.where("population", ">", 2_500_000).order("population").limit(2)

עם זאת, אם יש לכם מסנן עם השוואה לטווח (<,‏ <=,‏ >,‏ >=), הסדר הראשון צריך להיות באותו שדה. תוכלו לעיין ברשימת המגבלות של orderBy() בהמשך.

מגבלות

חשוב לשים לב להגבלה הבאה לגבי תנאי orderBy():

orderBy וקיומ

כשמגדירים שאילתות לפי שדה נתון, השאילתה יכולה להחזיר רק את המסמכים שבהם השדה לפיו מגדירים את הסדר קיים.

לדוגמה, השאילתה הבאה לא תחזיר מסמכים שבהם השדה population לא מוגדר, גם אם הם עומדים בכל שאר מסנני השאילתה.

Java
db.collection("cities").whereEqualTo("country", USA).orderBy(population);

השפעה קשורה חלה על אי-שוויון. שאילתה עם מסנן אי-שוויון בשדה מסוים כוללת גם סדר לפי השדה הזה. השאילתה הבאה לא מחזירה מסמכים ללא השדה population, גם אם הערך של country = USA במסמך הזה הוא null . כפתרון עקיף, אפשר להריץ שאילתות נפרדות לכל סדר או להקצות ערך לכל השדות לפי הסדר.

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);