Google致力於提高黑人社區的種族平等。 怎麼看。
本頁面由 Cloud Translation API 翻譯而成。
Switch to English

使用Cloud Firestore訂購和限制數據

Cloud Firestore提供了強大的查詢功能,用於指定您要從集合中檢索哪些文檔。這些查詢還可以與get()addSnapshotListener() ,如Get Data中所述

訂單和限制數據

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

例如,您可以按字母順序查詢前三個城市:

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

爪哇

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

Kotlin + KTX

citiesRef.orderBy("name").limit(3)
爪哇
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);
蟒蛇
db.collection(u'cities').order_by(u'name').limit(3).stream()
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)
的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個城市:

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

Kotlin + KTX

citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
爪哇
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
蟒蛇
cities_ref = db.collection(u'cities')
query = cities_ref.order_by(
    u'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
$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)

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

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

Kotlin + KTX

citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
爪哇
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
蟒蛇
cities_ref = db.collection(u'cities')
cities_ref.order_by(u'state').order_by(
    u'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
$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() 。在以下示例中,查詢定義人口閾值,按人口以升序排序,僅返回超過閾值的前幾個結果:

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

Kotlin + KTX

citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
爪哇
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
蟒蛇
cities_ref = db.collection(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'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
$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在同一字段上

    網頁
    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");

    Kotlin + KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("population")
    爪哇
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
    
    蟒蛇
    cities_ref = db.collection(u'cities')
    query = cities_ref.where(
        u'population', u'>', 2500000).order_by(u'population')
    results = query.stream()
    
    Node.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按不同字段

    網頁
    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");

    Kotlin + KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("country")
    爪哇
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
    
    蟒蛇
    cities_ref = db.collection(u'cities')
    query = cities_ref.where(u'population', u'>', 2500000).order_by(u'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
    $query = $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子句中包含的任何字段對查詢進行排序。