对数据进行排序并限制其数量

Firestore 提供了强大的查询功能,用于指定您要从集合中检索哪些文档。这些查询也可以与 get()addSnapshotListener() 结合使用,如获取数据中所述。

对数据进行排序和限制其数量

默认情况下,查询按文档 ID 以升序检索所有符合查询条件的文档。您可以使用 orderBy() 指定数据的排序顺序,也可以使用 limit() 限制检索到的文档数量。

例如,您可以通过以下代码查询按字母顺序排列的前 3 个城市:

Web
citiesRef.orderBy("name").limit(3)
Swift
citiesRef.order(by: "name").limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
  
Java
Android
citiesRef.orderBy("name").limit(3);
Kotlin+KTX
Android
citiesRef.orderBy("name").limit(3)
Java
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);
Python
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();
Go
query := cities.OrderBy("name", firestore.Asc).Limit(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)

您也可以按降序排序以获取最后 3 个城市:

Web
citiesRef.orderBy("name", "desc").limit(3)
Swift
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
  
Java
Android
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Kotlin+KTX
Android
citiesRef.orderBy("name", Query.Direction.DESCENDING).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()
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)

您还可以按多个字段排序。例如,如果您想要按州排序,并在每一个州按人口数量以降序排序,可使用以下代码:

Web
citiesRef.orderBy("state").orderBy("population", "desc")
Swift
citiesRef
    .order(by: "state")
    .order(by: "population", descending: true)
Objective-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
  
Java
Android
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Kotlin+KTX
Android
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
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)
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")

您可以将 where() 过滤条件与 orderBy()limit() 结合使用。下例中的查询定义了一个人口数量阈值,并按人口数量升序显示超过该阈值的前几个结果:

Web
citiesRef.where("population", ">", 100000).orderBy("population").limit(2)
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
    .limit(to: 2)
Objective-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"]
    queryLimitedTo:2];
  
Java
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Kotlin+KTX
Android
citiesRef.whereGreaterThan("population", 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()
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)

但是,如果您的过滤条件带有范围比较关系运算符(<<=>>=),您的第一重排序必须针对同一个字段,请参阅下面的 orderBy() 限制列表。

限制

请注意 orderBy() 子句的以下限制:

  • orderBy() 子句也会针对是否存在给定字段进行过滤。不含给定字段的文档将不包括在结果集中。
  • 如果您添加的过滤条件带有范围比较关系运算符(<<=>>=),您的第一重排序必须针对同一个字段:

    有效:范围过滤条件和 orderBy 针对的是同一个字段

    Web
    citiesRef.where("population", ">", 100000).orderBy("population")
    
    Swift
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "population")
    
    Objective-C
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
        queryOrderedByField:@"population"];
      
    Java
    Android
    citiesRef.whereGreaterThan("population", 100000).orderBy("population");
    Kotlin+KTX
    Android
    citiesRef.whereGreaterThan("population", 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()
    
    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")
    

    无效:范围过滤条件和第一个 orderBy 针对的是不同字段

    Web
    citiesRef.where("population", ">", 100000).orderBy("country")
    
    Swift
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "country")
    
    Objective-C
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
      
    Java
    Android
    citiesRef.whereGreaterThan("population", 100000).orderBy("country");
    Kotlin+KTX
    Android
    citiesRef.whereGreaterThan("population", 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()
    
    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
    $query = $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")
    
  • 您不能按等式(=) 或 in 子句中包含的任何字段对查询进行排序。