通过 Cloud Firestore 对数据排序和限定数量

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

对数据排序和限定数量

Cloud Firestore 还允许您使用 orderBy() 来指定数据的排列顺序,以及使用 limit() 来指定要检索的文档数量上限。例如,您可以通过以下代码查询按字母顺序排列的前 3 个城市:

网页
citiesRef.orderBy("name").limit(3)
Swift
citiesRef.order(by: "name").limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
  
Android
citiesRef.orderBy("name").limit(3);
Java
Query query = cities.orderBy("name").limit(3);
Python
db.collection(u'cities').order_by(u'name').limit(3).get()
Node.js
var firstThree = citiesRef.orderBy('name').limit(3);
Go
query := cities.OrderBy("name", firestore.Asc).Limit(3)
PHP
$query = $citiesRef->orderBy('name')->limit(3);

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

网页
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];
  
Android
citiesRef.orderBy("name", 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.get()
Node.js
var lastThree = citiesRef.orderBy('name', 'desc').limit(3);
Go
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP
$query = $citiesRef->orderBy('name', 'DESC')->limit(3);

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

网页
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];
  
Android
citiesRef.orderBy("state").orderBy("population", 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
var 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');

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

网页
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];
  
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.get()
Node.js
var 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);

但是,如果您的过滤条件带有范围比较关系运算符(<<=>>=),则您的第一重排序必须针对同一个字段:

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

网页
citiesRef.where("population", ">", 100000).orderBy("population")
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
Objective-C
[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"];
  
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.get()
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');

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

网页
citiesRef.where("population", ">", 100000).orderBy("country")
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "country")
Objective-C
[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
  
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.get()
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');

发送以下问题的反馈:

此网页
需要帮助?请访问我们的支持页面