Cloud Firestore cung cấp chức năng truy vấn mạnh mẽ để chỉ định tài liệu nào bạn muốn truy xuất từ bộ sưu tập. Những truy vấn này cũng có thể được sử dụng với get()
hoặc addSnapshotListener()
, như được mô tả trong Get Data .
Đặt hàng và giới hạn dữ liệu
Theo mặc định, một truy vấn sẽ truy xuất tất cả các tài liệu đáp ứng truy vấn theo thứ tự tăng dần theo ID tài liệu. Bạn có thể chỉ định thứ tự sắp xếp cho dữ liệu của mình bằng cách sử dụng orderBy()
và bạn có thể giới hạn số lượng tài liệu được truy xuất bằng cách sử dụng limit()
. Nếu bạn chỉ định limit()
thì giá trị phải lớn hơn hoặc bằng 0.
Ví dụ: bạn có thể truy vấn 3 thành phố đầu tiên theo thứ tự bảng chữ cái với:
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web namespaced API
citiesRef.orderBy("name").limit(3);
Nhanh
citiesRef.order(by: "name").limit(to: 3)
Mục tiêu-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
Đi
PHP
PHP
Để biết thêm về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .
Đoàn kết
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
hồng ngọc
Bạn cũng có thể sắp xếp theo thứ tự giảm dần để có được 3 thành phố cuối cùng :
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web namespaced API
citiesRef.orderBy("name", "desc").limit(3);
Nhanh
citiesRef.order(by: "name", descending: true).limit(to: 3)
Mục tiêu-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
Đi
PHP
PHP
Để biết thêm về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .
Đoàn kết
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
hồng ngọc
Bạn cũng có thể đặt hàng theo nhiều lĩnh vực. Ví dụ: nếu bạn muốn sắp xếp theo tiểu bang và trong mỗi tiểu bang, hãy sắp xếp theo dân số theo thứ tự giảm dần:
Web modular API
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web namespaced API
citiesRef.orderBy("state").orderBy("population", "desc");
Nhanh
citiesRef .order(by: "state") .order(by: "population", descending: true)
Mục tiêu-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
Đi
PHP
PHP
Để biết thêm về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .
Đoàn kết
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
hồng ngọc
Bạn có thể kết hợp các bộ lọc where()
với orderBy()
và limit()
. Trong ví dụ sau, các truy vấn xác định ngưỡng tổng thể, sắp xếp theo tổng thể theo thứ tự tăng dần và chỉ trả về một vài kết quả đầu tiên vượt quá ngưỡng:
Web modular API
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
Nhanh
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
Mục tiêu-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
Đi
PHP
PHP
Để biết thêm về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .
Đoàn kết
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
C#
hồng ngọc
Tuy nhiên, nếu bạn có bộ lọc có so sánh phạm vi ( <
, <=
, >
, >=
), đơn hàng đầu tiên của bạn phải nằm trên cùng một trường, hãy xem danh sách giới hạn orderBy()
bên dưới.
Hạn chế
Lưu ý các hạn chế sau đây đối với mệnh đề orderBy()
:
- Mệnh đề
orderBy()
cũng lọc sự tồn tại của các trường đã cho . Tập kết quả sẽ không bao gồm các tài liệu không chứa các trường nhất định. Nếu bạn bao gồm bộ lọc có so sánh phạm vi (
<
,<=
,>
,>=
), đơn hàng đầu tiên của bạn phải nằm trên cùng một trường:Hợp lệ : Bộ lọc phạm vi và
orderBy
trên cùng một trườngWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("population");
Nhanh
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
Mục tiêu-C
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và 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");
Java
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
Python
Python
Node.js
Đi
PHP
PHP
Để biết thêm về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .
Đoàn kết
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
C#
hồng ngọc
Không hợp lệ : Bộ lọc phạm vi và
orderBy
đầu tiênBy trên các trường khác nhauWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("country");
Nhanh
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
Mục tiêu-C
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và 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");
Java
Python
Python
C++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
Node.js
Đi
PHP
PHP
Để biết thêm về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .
Đoàn kết
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
C#
hồng ngọc
orderBy
và sự tồn tại
Khi bạn sắp xếp một truy vấn theo một trường nhất định, truy vấn đó chỉ có thể trả về những tài liệu có tồn tại trường theo thứ tự đó.
Ví dụ: truy vấn sau sẽ không trả về bất kỳ tài liệu nào mà trường population
không được đặt, ngay cả khi chúng đáp ứng các bộ lọc truy vấn.
Java
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
Một hiệu ứng liên quan áp dụng cho sự bất bình đẳng. Truy vấn có bộ lọc bất đẳng thức trên một trường cũng bao hàm việc sắp xếp theo trường đó. Truy vấn sau đây không trả về tài liệu không có trường population
ngay cả khi country = USA
trong tài liệu đó. Để khắc phục, bạn có thể thực hiện các truy vấn riêng biệt cho từng đơn hàng hoặc bạn có thể chỉ định một giá trị cho tất cả các trường mà bạn sắp xếp theo đó.
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
Truy vấn ở trên bao gồm thứ tự ngụ ý về bất đẳng thức và tương đương với truy vấn sau:
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);