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

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

הזמנה והגבלה של נתונים

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

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

Web

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

const q = query(citiesRef, orderBy("name"), limit(3));

Web

citiesRef.orderBy("name").limit(3);
Swift
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
citiesRef.order(by: "name").limit(to: 3)
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
[[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
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);
Python
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = query.get()

Python

cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = await query.get()
C++‎
cities_ref.OrderBy("name").Limit(3);
Node.js
const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();
Go
query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestore.Asc).LimitToLast(3)
PHP

PHP

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

$query = $citiesRef->orderBy('name')->limit(3);
Unity
Query query = citiesRef.OrderBy("Name").Limit(3);
C#‎
Query query = citiesRef.OrderBy("Name").Limit(3);
Ruby
query = cities_ref.order("name").limit(3)

אפשר גם למיין בסדר יורד כדי לקבל את 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
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
[[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
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
Python
cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()
C++‎
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
Go
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP

PHP

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

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

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

Web

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

const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));

Web

citiesRef.orderBy("state").orderBy("population", "desc");
Swift
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
citiesRef
  .order(by: "state")
  .order(by: "population", descending: true)
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
[[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
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
Python
cities_ref = db.collection("cities")
ordered_city_ref = cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)

Python

cities_ref = db.collection("cities")
cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)
C++‎
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
Go
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
PHP

PHP

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

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

אפשר לשלב בין מסנני 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
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
citiesRef
  .whereField("population", isGreaterThan: 100000)
  .order(by: "population")
  .limit(to: 2)
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS ובמטרות של קליפים של אפליקציות.
[[[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
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
Python
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()
C++‎
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
    .OrderBy("population")
    .Limit(2);
Node.js
const biggestRes = await citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2).get();
Go
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)
PHP

PHP

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

$query = $citiesRef
    ->where('population', '>', 2500000)
    ->orderBy('population')
    ->limit(2);
Unity
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
C#‎
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
Ruby
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 במסמך הזה . כפתרון עקיף, אפשר להריץ שאילתות נפרדות לכל סדר, או להקצות ערך לכל השדות שאתם מסדרים לפי.

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