获取我们在 Firebase 峰会上发布的所有信息,了解 Firebase 可如何帮助您加快应用开发速度并满怀信心地运行应用。了解详情

使用 Cloud Firestore 订购和限制数据

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

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

订单和限制数据

默认情况下,查询会按文档 ID 升序检索满足查询的所有文档。您可以使用orderBy()指定数据的排序顺序,并且可以使用limit()检索的文档数量。

例如,您可以按字母顺序查询前 3 个城市:

Web version 9

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

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

Web version 8

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

Kotlin+KTX

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

Java

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

Dart

final citiesRef = db.collection("cities");
citiesRef.orderBy("name").limit(3);
爪哇
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);
节点.js
const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();
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);
统一
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Query query = citiesRef.OrderBy("Name").Limit(3);
红宝石
query = cities_ref.order("name").limit(3)

您还可以按降序排序以获得最后3 个城市:

Web version 9

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

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

Web version 8

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

Kotlin+KTX

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);
爪哇
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()

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);
节点.js
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP

PHP

有关安装和创建 Cloud Firestore 客户端的更多信息,请参阅Cloud Firestore 客户端库

$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)

您还可以按多个字段排序。例如,如果您想按州排序,并在每个州内按人口降序排序:

Web version 9

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

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

Web version 8

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

Kotlin+KTX

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);
爪哇
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)

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);
节点.js
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
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');
统一
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()结合使用。在下面的例子中,查询定义了一个人口阈值,按人口升序排序,只返回前几个超过阈值的结果:

Web version 9

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

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

Web version 8

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

Kotlin+KTX

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);
爪哇
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()

Python

cities_ref = db.collection("cities")
query = cities_ref.where("population", ">", 2500000).order_by("population").limit(2)
results = query.stream()
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
    .OrderBy("population")
    .Limit(2);
节点.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

PHP

有关安装和创建 Cloud Firestore 客户端的更多信息,请参阅Cloud Firestore 客户端库

$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

    Web version 9

    import { query, where, orderBy } from "firebase/firestore";  
    
    const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));

    Web version 8

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

    Kotlin+KTX

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

    Java

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

    Dart

    final citiesRef = db.collection("cities");
    citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
    爪哇
    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()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("population")
    results = query.stream()
    节点.js
    citiesRef.where('population', '>', 2500000).orderBy('population');
    query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
    PHP

    PHP

    有关安装和创建 Cloud Firestore 客户端的更多信息,请参阅Cloud Firestore 客户端库

    $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

    Web version 9

    import { query, where, orderBy } from "firebase/firestore";  
    
    const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));

    Web version 8

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

    Kotlin+KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("country")

    Java

    citiesRef.whereGreaterThan("population", 100000).orderBy("country");

    Dart

    final citiesRef = db.collection("cities");
    citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
    爪哇
    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()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("country")
    results = query.stream()
    C++
    // BAD EXAMPLE -- will crash the program:
    cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
        .OrderBy("country");
    节点.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

    PHP

    有关安装和创建 Cloud Firestore 客户端的更多信息,请参阅Cloud Firestore 客户端库

    $invalidRangeQuery = $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包含的任何字段对查询进行排序。