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, truy vấn 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 một 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ự abc với:
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);
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
con trăn
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 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);
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
con trăn
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 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");
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
con trăn
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 bộ lọc where()
với orderBy()
và limit()
. Trong ví dụ sau, truy vấn xác định ngưỡng dân số, sắp xếp theo dân số 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 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);
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
con trăn
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 ( <
, <=
, >
, >=
), thứ tự đầu tiên của bạn phải 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 đố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 hợp kết quả sẽ không bao gồm các tài liệu không chứa các trường đã cho. Nếu bạn bao gồm bộ lọc có so sánh phạm vi (
<
,<=
,>
,>=
), thứ tự đầu tiên của bạn phải 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 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");
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");
con trăn
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ên trên các trường khác nhauWeb 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");
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
con trăn
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
- Bạn không thể sắp xếp truy vấn của mình theo bất kỳ trường nào có trong đẳng thức (
=
) hoặcin
mệnh đề.
orderBy
và tồn tại
Khi bạn sắp xếp một truy vấn theo một trường đã cho, truy vấn đó chỉ có thể trả về những tài liệu có trường sắp xếp theo thứ tự đó.
Ví dụ: truy vấn sau đây sẽ không trả về bất kỳ tài liệu nào không đặt trường population
, 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 bất bình đẳng. Truy vấn có bộ lọc bất bình đẳng trên một trường cũng ngụ ý sắp xếp thứ tự 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 đó. Như một giải pháp thay thế, bạn có thể thực hiện các truy vấn riêng biệt cho từng thứ tự hoặc bạn có thể gán một giá trị cho tất cả các trường mà bạn sắp xếp.
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
Truy vấn trên bao gồm một 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”);