Cloud Firestore zapewnia zaawansowaną funkcję zapytań do określania dokumentów, które chcesz pobrać z kolekcji. Tych zapytań można również używać z get()
lub addSnapshotListener()
, zgodnie z opisem w sekcji Get Data .
Zamów i ogranicz dane
Domyślnie zapytanie pobiera wszystkie dokumenty, które spełniają zapytanie, w porządku rosnącym według identyfikatora dokumentu. Możesz określić porządek sortowania swoich danych za pomocą orderBy()
, a także możesz ograniczyć liczbę dokumentów pobieranych za pomocą limit()
.
Na przykład możesz wyszukać alfabetycznie pierwsze 3 miasta za pomocą:
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);
Szybki
citiesRef.order(by: "name").limit(to: 3)
Cel C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Java
citiesRef.orderBy("name").limit(3);
Kotlin+KTX
citiesRef.orderBy("name").limit(3)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
Jawa
Pyton
Python
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
Udać się
PHP
$query = $citiesRef->orderBy('name')->limit(3);
Jedność
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Query query = citiesRef.OrderBy("Name").Limit(3);
Rubin
Możesz także posortować w kolejności malejącej, aby uzyskać 3 ostatnie miasta:
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);
Szybki
citiesRef.order(by: "name", descending: true).limit(to: 3)
Cel C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
Java
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Kotlin+KTX
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descending: true).limit(3);
Jawa
Pyton
Python
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
Udać się
PHP
$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
Jedność
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Query query = citiesRef.OrderByDescending("Name").Limit(3);
Rubin
Możesz także zamawiać według wielu pól. Na przykład, jeśli chcesz uporządkować według stanu, a w każdym stanie według populacji w porządku malejącym:
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");
Szybki
citiesRef .order(by: "state") .order(by: "population", descending: true)
Cel C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
Java
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Kotlin+KTX
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("population", descending: true);
Jawa
Pyton
Python
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
Udać się
PHP
$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
Jedność
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
Rubin
Możesz łączyć filtry where( where()
z orderBy()
i limit()
. W poniższym przykładzie zapytania definiują próg populacji, sortują według populacji w porządku rosnącym i zwracają tylko kilka pierwszych wyników, które przekraczają próg:
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);
Szybki
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
Cel C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
Dart
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("population") .limit(2);
Jawa
Pyton
Python
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
Udać się
PHP
$query = $citiesRef ->where('population', '>', 2500000) ->orderBy('population') ->limit(2);
Jedność
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
C#
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
Rubin
Jeśli jednak masz filtr z porównaniem zakresów ( <
, <=
, >
, >=
), Twoje pierwsze zamówienie musi znajdować się w tym samym polu, zobacz listę ograniczeń orderBy()
poniżej.
Ograniczenia
Zwróć uwagę na następujące ograniczenia dotyczące klauzul orderBy()
:
-
orderBy()
również filtruje istnienie danych pól. Zestaw wyników nie będzie zawierał dokumentów, które nie zawierają danych pól. Jeśli dodasz filtr z porównaniem zakresu (
<
,<=
,>
,>=
), pierwsze zamówienie musi znajdować się w tym samym polu:Prawidłowy : filtr zakresu i
orderBy
według tego samego polaWeb 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");
Szybki
Uwaga: ten produkt nie jest dostępny w systemach docelowych watchOS i App Clip.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
Cel C
Uwaga: ten produkt nie jest dostępny w systemach docelowych watchOS i App Clip.[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population")
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
Jawa
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
Pyton
Python
Node.js
Udać się
PHP
$query = $citiesRef ->where('population', '>', 2500000) ->orderBy('population');
Jedność
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
C#
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
Rubin
Nieprawidłowy : filtr zakresu i pierwsze
orderBy
według w różnych polachWeb 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");
Szybki
Uwaga: ten produkt nie jest dostępny w systemach docelowych watchOS i App Clip.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
Cel C
Uwaga: ten produkt nie jest dostępny w systemach docelowych watchOS i App Clip.[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("country")
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
Jawa
Pyton
Python
C++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
Node.js
Udać się
PHP
$invalidRangeQuery = $citiesRef ->where('population', '>', 2500000) ->orderBy('country');
Jedność
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
C#
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
Rubin
- Nie możesz uporządkować zapytania według żadnego pola zawartego w równości (
=
) lubin
klauzuli.