使用 Cloud Firestore 訂購和限制數據

Cloud Firestore 提供了強大的查詢功能,用於指定要從集合中檢索哪些文檔。這些查詢也可以與任何使用get()addSnapshotListener()如在獲取數據

訂購和限制數據

默認情況下,查詢按文檔 ID 按升序檢索滿足查詢的所有文檔。您可以使用指定為您的數據的排序順序orderBy()你可以限制使用檢索到的文件的數量limit()

例如,您可以使用以下字母順序查詢前 3 個城市:

網頁版 9

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

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

網頁版 8

citiesRef.orderBy("name").limit(3);
迅速
citiesRef.order(by: "name").limit(to: 3)
目標-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];

爪哇

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

科特林+KTX

citiesRef.orderBy("name").limit(3)
爪哇
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);
節點.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
$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個城市可以:

網頁版 9

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

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

網頁版 8

citiesRef.orderBy("name", "desc").limit(3);
迅速
citiesRef.order(by: "name", descending: true).limit(to: 3)
目標-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];

爪哇

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

科特林+KTX

citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
爪哇
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);
節點.js
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP
$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)

您還可以按多個字段排序。例如,如果您想按州排序,並在每個州內按人口降序排序:

網頁版 9

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

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

網頁版 8

citiesRef.orderBy("state").orderBy("population", "desc");
迅速
citiesRef
    .order(by: "state")
    .order(by: "population", descending: true)
目標-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];

爪哇

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

科特林+KTX

citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
爪哇
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);
節點.js
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
PHP
$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()在以下示例中,查詢定義了人口閾值,按人口升序排序,並僅返回超過閾值的前幾個結果:

網頁版 9

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

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

網頁版 8

citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
迅速
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
    .limit(to: 2)
目標-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"]
    queryLimitedTo:2];

爪哇

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

科特林+KTX

citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
爪哇
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);
節點.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
$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在同一領域

    網頁版 9

    import { query, where, orderBy } from "firebase/firestore";  
    
    const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));

    網頁版 8

    citiesRef.where("population", ">", 100000).orderBy("population");
    迅速
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "population")
    目標-C
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
        queryOrderedByField:@"population"];

    爪哇

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

    科特林+KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("population")
    爪哇
    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()
    節點.js
    citiesRef.where('population', '>', 2500000).orderBy('population');
    query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
    PHP
    $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不同的領域

    網頁版 9

    import { query, where, orderBy } from "firebase/firestore";  
    
    const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));

    網頁版 8

    citiesRef.where("population", ">", 100000).orderBy("country");
    迅速
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "country")
    目標-C
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];

    爪哇

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

    科特林+KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("country")
    爪哇
    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");
    節點.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
    $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")
  • 你可以不包括在平等的任何字段(訂單查詢= ),或in條款。