Cloud Firestore は、コレクションから取得するドキュメントを指定するための強力なクエリ機能を提供します。これらのクエリは、 Get Dataで説明されているように、 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);
迅速
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);
ジャワ
パイソン
Python
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
行け
PHP
PHP
Cloud Firestore クライアントのインストールと作成の詳細については、 Cloud Firestore クライアント ライブラリを参照してください。
団結
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
ルビー
降順で並べ替えて、最後の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);
迅速
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);
ジャワ
パイソン
Python
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
行け
PHP
PHP
Cloud Firestore クライアントのインストールと作成の詳細については、 Cloud Firestore クライアント ライブラリを参照してください。
団結
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
ルビー
複数のフィールドで注文することもできます。たとえば、州ごとに並べ替え、各州内で人口の降順で並べ替える場合は、次のようにします。
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");
迅速
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);
ジャワ
パイソン
Python
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
行け
PHP
PHP
Cloud Firestore クライアントのインストールと作成の詳細については、 Cloud Firestore クライアント ライブラリを参照してください。
団結
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
ルビー
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);
迅速
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);
ジャワ
パイソン
Python
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
行け
PHP
PHP
Cloud Firestore クライアントのインストールと作成の詳細については、 Cloud Firestore クライアント ライブラリを参照してください。
団結
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
C#
ルビー
ただし、範囲比較 ( <
、 <=
、 >
、 >=
) を使用するフィルターがある場合、最初の順序付けは同じフィールドで行う必要があります。以下の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")
Objective-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
Node.js
行け
PHP
PHP
Cloud Firestore クライアントのインストールと作成の詳細については、 Cloud Firestore クライアント ライブラリを参照してください。
団結
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
C#
ルビー
無効: 異なるフィールドの範囲フィルターと最初の
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")
Objective-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");
ジャワ
パイソン
Python
C++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
Node.js
行け
PHP
PHP
Cloud Firestore クライアントのインストールと作成の詳細については、 Cloud Firestore クライアント ライブラリを参照してください。
団結
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
C#
ルビー
- 等式 (
=
) または句in
含まれるフィールドでクエリを並べ替えることはできません。