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

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

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

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

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

Web modular API

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

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

Web namespaced API

citiesRef.orderBy("name").limit(3);
מָהִיר
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
citiesRef.order(by: "name").limit(to: 3)
Objective-C
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
[[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
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()

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();
ללכת
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);
אַחְדוּת
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Query query = citiesRef.OrderBy("Name").Limit(3);
אוֹדֶם
query = cities_ref.order("name").limit(3)

תוכל גם למיין בסדר יורד כדי לקבל את 3 הערים האחרונות :

Web modular API

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

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

Web namespaced API

citiesRef.orderBy("name", "desc").limit(3);
מָהִיר
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
[[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
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()

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();
ללכת
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP

PHP

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

$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
אַחְדוּת
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Query query = citiesRef.OrderByDescending("Name").Limit(3);
אוֹדֶם
query = cities_ref.order("name", "desc").limit(3)

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

Web modular API

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

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

Web namespaced API

citiesRef.orderBy("state").orderBy("population", "desc");
מָהִיר
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
citiesRef
  .order(by: "state")
  .order(by: "population", descending: true)
Objective-C
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
[[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
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
פִּיתוֹן
cities_ref = db.collection("cities")
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();
ללכת
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');
אַחְדוּת
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
אוֹדֶם
query = cities_ref.order("state").order("population", "desc")

אתה יכול לשלב מסנני where() עם orderBy() ו- limit() . בדוגמה הבאה, השאילתות מגדירות סף אוכלוסייה, ממיינות לפי אוכלוסייה בסדר עולה ומחזירות רק את התוצאות הראשונות החורגות מהסף:

Web modular API

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

const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));

Web namespaced API

citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
מָהִיר
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
citiesRef
  .whereField("population", isGreaterThan: 100000)
  .order(by: "population")
  .limit(to: 2)
Objective-C
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
[[[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
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()

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();
ללכת
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);
אַחְדוּת
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
C#
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() מסננת גם את קיומם של השדות הנתונים . ערכת התוצאות לא תכלול מסמכים שאינם מכילים את השדות הנתונים.
  • אם אתה כולל מסנן עם השוואת טווח ( < , <= , > , >= ), ההזמנה הראשונה שלך חייבת להיות באותו שדה:

    תקף : מסנן טווח ו- 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");
    פִּיתוֹן
    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "population"
    )
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "population"
    )
    results = query.stream()
    Node.js
    citiesRef.where('population', '>', 2500000).orderBy('population');
    ללכת
    query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
    PHP

    PHP

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

    $query = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('population');
    אַחְדוּת
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    C#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    אוֹדֶם
    query = cities_ref.where("population", ">", 2_500_000).order("population")

    לא חוקי : מסנן טווח 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
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
    פִּיתוֹן
    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "country"
    )
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "country"
    )
    results = query.stream()
    C++
    // BAD EXAMPLE -- will crash the program:
    cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
        .OrderBy("country");
    Node.js
    citiesRef.where('population', '>', 2500000).orderBy('country');
    ללכת
    // Note: This is an invalid query. It violates the constraint that range
    // and order by are required to be on the same field.
    query := cities.Where("population", ">", 2500000).OrderBy("country", firestore.Asc)
    PHP

    PHP

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

    $invalidRangeQuery = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('country');
    אַחְדוּת
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    C#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    אוֹדֶם
    query = cities_ref.where("population", ">", 2_500_000).order("country")

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

השאילתה שלמעלה כוללת סדר מרומז על אי השוויון והיא שווה ערך לדברים הבאים:

Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);