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

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

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

默认情况下,查询会按文档 ID 以升序检索符合查询条件的所有文档。您可以使用 orderBy() 指定数据的排序顺序,并可使用 limit() 限制检索到的文档数量。如果您指定 limit(),其值必须大于或等于零。

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

Web

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

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

Web

citiesRef.orderBy("name").limit(3);
Swift
注意:此产品不适用于 watchOS 和 App Clip 目标。
citiesRef.order(by: "name").limit(to: 3)
Objective-C
注意:此产品不适用于 watchOS 和 App Clip 目标。
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];

Kotlin

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

Java

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

Dart

final citiesRef = db.collection("cities");
citiesRef.orderBy("name").limit(3);
Java
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);
Node.js
const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();
Go
query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestore.Asc).LimitToLast(3)
PHP

PHP

如需详细了解如何安装和创建 Cloud Firestore 客户端,请参阅 Cloud Firestore 客户端库

$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

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

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

Web

citiesRef.orderBy("name", "desc").limit(3);
Swift
注意:此产品不适用于 watchOS 和 App Clip 目标。
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
注意:此产品不适用于 watchOS 和 App Clip 目标。
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];

Kotlin

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

Java

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

Dart

final citiesRef = db.collection("cities");
citiesRef.orderBy("name", descending: true).limit(3);
Java
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
Python
cities_ref = db.collection("cities")
query = cities_ref.order_by("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);
Node.js
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
Go
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP

PHP

如需详细了解如何安装和创建 Cloud Firestore 客户端,请参阅 Cloud Firestore 客户端库

$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

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

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

Web

citiesRef.orderBy("state").orderBy("population", "desc");
Swift
注意:此产品不适用于 watchOS 和 App Clip 目标。
citiesRef
  .order(by: "state")
  .order(by: "population", descending: true)
Objective-C
注意:此产品不适用于 watchOS 和 App Clip 目标。
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];

Kotlin

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

Java

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

Dart

final citiesRef = db.collection("cities");
citiesRef.orderBy("state").orderBy("population", descending: true);
Java
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
Python
cities_ref = db.collection("cities")
ordered_city_ref = cities_ref.order_by("state").order_by(
    "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);
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

PHP

如需详细了解如何安装和创建 Cloud Firestore 客户端,请参阅 Cloud Firestore 客户端库

$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

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

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

Web

citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
Swift
注意:此产品不适用于 watchOS 和 App Clip 目标。
citiesRef
  .whereField("population", isGreaterThan: 100000)
  .order(by: "population")
  .limit(to: 2)
Objective-C
注意:此产品不适用于 watchOS 和 App Clip 目标。
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"]
    queryLimitedTo:2];

Kotlin

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

Java

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

Dart

final citiesRef = db.collection("cities");
citiesRef
    .where("population", isGreaterThan: 100000)
    .orderBy("population")
    .limit(2);
Java
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
Python
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("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

PHP

如需详细了解如何安装和创建 Cloud Firestore 客户端,请参阅 Cloud Firestore 客户端库

$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 及其存在性

当您按给定字段对查询进行排序时,查询只会返回存在排序依据 (order-by) 字段的文档。

例如,以下查询不会返回任何未设置 population 字段的文档,即使它们符合查询过滤条件也是如此。

Java
db.collection("cities").whereEqualTo("country", USA).orderBy(population);

类似的规则也适用于不等式。如果查询对某个字段使用不等式过滤条件,则系统会按该字段进行排序。以下查询不会返回不含 population 字段的文档,即使该文档中有 country = USA 也是如此。如需解决此问题,您可以为每个排序执行单独的查询,也可以为所有排序依据字段分配一个值。

Java
db.collection(cities).where(or(country, USA), greaterThan(population, 250000));

上面查询中的不等式隐含了排序依据 (order-by),它等效于下面的查询:

Java
db.collection(cities).where(or(country, USA), greaterThan(population, 250000)).orderBy(population);