Cloud Firestore 提供強大的查詢功能,可讓您指定
要從集合擷取的文件。您也可以使用這些查詢
搭配 get()
或 addSnapshotListener()
使用,方法如「取得
資料:
訂單和限制資料
根據預設,查詢會擷取符合查詢的所有文件 (遞增
排序查詢您可以使用
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
citiesRef.order(by: "name").limit(to: 3)
Objective-C
[[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);
Java
Python
Python
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
Go
PHP
PHP
如要進一步瞭解如何安裝及建立 Cloud Firestore 用戶端,請參閱 Cloud Firestore 用戶端程式庫。
Unity
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Ruby
此外,您也可以遞減排序,取得「最近」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
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
[[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);
Java
Python
Python
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
Go
PHP
PHP
如要進一步瞭解如何安裝及建立 Cloud Firestore 用戶端,請參閱 Cloud Firestore 用戶端程式庫。
Unity
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Ruby
您也可以依多個欄位排序。舉例來說,假設您要依據 並按母體以遞減方式排序:
Web
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
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];
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);
Java
Python
Python
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
Go
PHP
PHP
如要進一步瞭解如何安裝及建立 Cloud Firestore 用戶端,請參閱 Cloud Firestore 用戶端程式庫。
Unity
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Ruby
您可以將 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
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
Objective-C
[[[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);
Java
Python
Python
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
Go
PHP
PHP
如要進一步瞭解如何安裝及建立 Cloud Firestore 用戶端,請參閱 Cloud 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”);
相關效應適用於不等式。使用不等式篩選條件的查詢
也會表示按照該欄位排序。下列
查詢不會傳回不含 population
欄位的文件,
如果文件包含 country = USA
標記。做為替代方案
分別為每種排序查詢,您也可以為所有欄位指定值
做為排序依據
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
上述查詢包含不相等的隱含順序, 相當於以下程式碼:
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);