Bergabunglah dengan kami secara langsung dan online di Firebase Summit pada 18 Oktober 2022. Pelajari cara Firebase dapat membantu Anda mempercepat pengembangan aplikasi, merilis aplikasi dengan percaya diri, dan menentukan skala dengan mudah. Daftar sekarang

Mengurutkan dan membatasi data dengan Cloud Firestore

Tetap teratur dengan koleksi Simpan dan kategorikan konten berdasarkan preferensi Anda.

Cloud Firestore menyediakan fungsionalitas kueri yang dapat diandalkan untuk menentukan dokumen mana yang ingin Anda ambil dari koleksi. Kueri ini juga dapat digunakan dengan get() atau addSnapshotListener(), seperti yang dijelaskan dalam artikel Mendapatkan Data.

Mengurutkan dan membatasi data

Secara default, suatu kueri akan mengambil semua dokumen yang memenuhi kueri tersebut dalam urutan menaik menurut ID dokumen. Anda dapat menentukan tata urutan data Anda menggunakan orderBy(), dan membatasi jumlah dokumen yang diambil menggunakan limit().

Misalnya, Anda dapat mengajukan kueri untuk 3 kota pertama menurut abjad dengan:

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);
Swift
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
citiesRef.order(by: "name").limit(to: 3)
Objective-C
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];

Java

citiesRef.orderBy("name").limit(3);

Kotlin+KTX

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

Anda juga dapat mengurutkan dalam urutan menurun untuk mendapatkan 3 kota terakhir:

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);
Swift
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];

Java

citiesRef.orderBy("name", Direction.DESCENDING).limit(3);

Kotlin+KTX

citiesRef.orderBy("name", Query.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(u'cities')
query = cities_ref.order_by(
    u'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
$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)

Anda juga dapat mengurutkan berdasarkan beberapa kolom. Misalnya, jika Anda ingin mengurutkan berdasarkan negara bagian, dan dalam setiap negara bagian mengurutkan berdasarkan populasi dalam urutan menurun:

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");
Swift
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
citiesRef
    .order(by: "state")
    .order(by: "population", descending: true)
Objective-C
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];

Java

citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);

Kotlin+KTX

citiesRef.orderBy("state").orderBy("population", Query.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(u'cities')
cities_ref.order_by(u'state').order_by(
    u'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
$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")

Anda dapat menggabungkan filter where() dengan orderBy() dan limit(). Dalam contoh berikut, kueri menentukan nilai minimum populasi, mengurutkan berdasarkan populasi dalam urutan menaik, dan hanya menampilkan beberapa hasil pertama yang melampaui nilai minimum tersebut:

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);
Swift
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
    .limit(to: 2)
Objective-C
Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"]
    queryLimitedTo:2];

Java

citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);

Kotlin+KTX

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(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'population').limit(2)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = cities_ref.where("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
$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)

Namun, jika Anda memiliki filter dengan perbandingan rentang (<, <=, >, >=), pengurutan pertama harus berada di kolom yang sama. Lihat daftar batasan orderBy() di bawah ini.

Batasan

Perhatikan batasan berikut untuk klausa orderBy():

  • Klausa orderBy() juga memfilter keberadaan kolom yang dimaksud. Kumpulan hasilnya tidak akan menyertakan dokumen yang tidak berisi kolom yang dimaksud.
  • Jika Anda menyertakan filter dengan perbandingan rentang (<, <=, >, >=), pengurutan pertama harus berada di kolom yang sama:

    Valid: Filter rentang dan orderBy berada di kolom yang sama

    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");
    Swift
    Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "population")
    Objective-C
    Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
        queryOrderedByField:@"population"];

    Java

    citiesRef.whereGreaterThan("population", 100000).orderBy("population");

    Kotlin+KTX

    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
    cities_ref = db.collection(u'cities')
    query = cities_ref.where(
        u'population', u'>', 2500000).order_by(u'population')
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("population")
    results = query.stream()
    Node.js
    citiesRef.where('population', '>', 2500000).orderBy('population');
    Go
    query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
    PHP
    $query = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('population');
    Unity
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    C#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    Ruby
    query = cities_ref.where("population", ">", 2_500_000).order("population")

    Tidak valid: Filter rentang dan orderBy pertama berada di kolom berbeda

    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");
    Swift
    Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "country")
    Objective-C
    Catatan: Produk ini tidak tersedia di target watchOS dan App Clip.
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];

    Java

    citiesRef.whereGreaterThan("population", 100000).orderBy("country");

    Kotlin+KTX

    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");
    Python
    cities_ref = db.collection(u'cities')
    query = cities_ref.where(u'population', u'>', 2500000).order_by(u'country')
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where("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');
    Go
    // 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
    $invalidRangeQuery = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('country');
    Unity
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    C#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    Ruby
    query = cities_ref.where("population", ">", 2_500_000).order("country")
  • Anda tidak dapat mengurutkan kueri berdasarkan kolom apa pun yang disertakan dalam klausa kesetaraan (=) atau klausa in.