转到控制台

通过 Cloud Firestore 对数据进行排序和限制其数量

Cloud 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
citiesRef.orderBy("name").limit(3);
Kotlin
citiesRef.orderBy("name").limit(3)
Java
Query query = cities.orderBy("name").limit(3);
Python
db.collection(u'cities').order_by(u'name').limit(3).stream()
Node.js
let firstThree = citiesRef.orderBy('name').limit(3);
Go
query := cities.OrderBy("name", firestore.Asc).Limit(3)
PHP
$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
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Kotlin
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()
Node.js
let lastThree = citiesRef.orderBy('name', 'desc').limit(3);
Go
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP
$query = $citiesRef->orderBy('name', 'DESC')->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
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Kotlin
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)
Node.js
let byStateByPop = citiesRef.orderBy('state').orderBy('population', 'desc');
Go
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
PHP
$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
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
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Kotlin
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()
Node.js
let biggest = citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2);
Go
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)
PHP
$query = $citiesRef
    ->where('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 针对的是同一个字段

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
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Kotlin
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');
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
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Kotlin
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()
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');
C#
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Country");
Ruby
query = cities_ref.where("population", ">", 2_500_000).order("country")