排序及限制資料
Firestore 提供強大的查詢功能,可指定要從集合中擷取的檔案。如「取得資料」一文所述,這些查詢也可以搭配 get() 或 addSnapshotListener() 使用。
排序及限制資料
根據預設,查詢會擷取所有符合查詢條件的文件,並依文件 ID 升序排序。您可以使用 orderBy() 指定資料的排序順序,並使用 limit() 限制擷取的檔案數量。如要指定 limit(),值必須大於或等於零。
舉例來說,您可以查詢前 3 個依字母順序排列的城市:
網頁版 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("nam);order_and_limit.js
網頁版 8
citiesRef.orderBy("name")restore.js
Swift
citiesRef.order(by: "name").liller.swift
Objective-C
[[citiesRef queryOrderedByField:@"name"] queryLimntroller.m
Kotlin
Android
citiesRef.orderBy("name"nippets.kt
Java
Android
citiesRef.orderBy("name")ppets.java
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("nit(3);firestore.dart
Java
Python
Python
(Async)
C++
cities_ref.OrderBy("name")ippets.cpp
Node.js
Go
PHP
PHP
如要向 Firestore 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Unity
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Ruby
你也可以依遞減順序排序,取得最後 3 個城市:
網頁版 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name",it(3));order_and_limit_desc.js
網頁版 8
citiesRef.orderBy("name", "d3);test.firestore.js
Swift
citiesRef.order(by: "name", descending: true).liller.swift
Objective-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimntroller.m
Kotlin
Android
citiesRef.orderBy("name", Query.Direction.DESCENDINGnippets.kt
Java
Android
citiesRef.orderBy("name", Direction.DESCENDING)ppets.java
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descendit(3);firestore.dart
Java
Python
Python
(Async)
C++
cities_ref.OrderBy("name", Query::Direction::kDescending)ippets.cpp
Node.js
Go
PHP
PHP
如要向 Firestore 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Unity
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Ruby
您也可以依多個欄位排序。舉例來說,假設您想依州別排序,並在每個州別中依人口數遞減排序:
網頁版 9
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy(&qut;, "desc"));order_multiple.js
網頁版 8
citiesRef.orderBy("state").orderBy("popula;desc");test.firestore.js
Swift
citiesRef .order(by: "state") .order(by: "population"ViewController.swift
Objective-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population&quES];ViewController.m
Kotlin
Android
citiesRef.orderBy("state").orderBy("population", Query.DNDING)DocSnippets.kt
Java
Android
citiesRef.orderBy("state").orderBy("population", DiNG);DocSnippets.java
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("populascending: true);firestore.dart
Java
Python
Python
(Async)
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direending);snippets.cpp
Node.js
Go
PHP
PHP
如要向 Firestore 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Unity
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Ruby
您可以將 where() 篩選器與 orderBy() 和 limit() 結合使用。在下列範例中,查詢會定義人口數門檻、依人口數遞增排序,並只傳回超過門檻的前幾筆結果:
網頁版 9
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where(">;population", "", 100000), ordon"), limit(2));filter_and_order.js
網頁版 8
citiesRef.where("populatio>n", "", 100000).orderBy(&qut;).limit(2);test.firestore.js
Swift
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population&quViewController.swift
Objective-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] :2];ViewController.m
Kotlin
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("populamit(2)DocSnippets.kt
Java
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("populat(2);DocSnippets.java
Dart
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("po) .limit(2);firestore.dart
Java
Python
Python
(Async)
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population&qimit(2);snippets.cpp
Node.js
Go
PHP
PHP
如要向 Firestore 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Unity
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
C#
Ruby
不過,如果篩選器包含範圍比較 (<、<=、>、>=),則第一個排序必須使用相同欄位,請參閱下方的orderBy()限制清單。
限制
請注意,orderBy() 子句有下列限制:
orderBy()子句也會篩選指定欄位的存在狀態。 結果集不會包含不含指定欄位的文件。
orderBy 和存在
依指定欄位排序查詢時,查詢只能傳回排序依據欄位存在的文件。
舉例來說,即使文件符合查詢篩選器,如果未設定 population 欄位,下列查詢也不會傳回任何文件。
Java
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
相關效應適用於不等式。如果查詢在欄位上使用不等式篩選器,也表示要依該欄位排序。即使文件中包含 country = USA,下列查詢也不會傳回沒有 population 欄位的文件。如要解決這個問題,您可以為每個排序執行個別查詢,也可以為所有排序依據的欄位指派值。
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
上述查詢包含不等式上的隱含排序依據,等同於下列查詢:
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);