Đặt hàng và giới hạn dữ liệu bằng Cloud Firestore

Cloud Firestore cung cấp chức năng truy vấn mạnh mẽ để chỉ định tài liệu bạn muốn truy xuất từ bộ sưu tập. Các truy vấn này cũng có thể được sử dụng với get() hoặc addSnapshotListener(), như được mô tả trong Lấy Dữ liệu.

Sắp xếp thứ tự và giới hạn dữ liệu

Theo mặc định, truy vấn sẽ truy xuất tất cả tài liệu đáp ứng truy vấn theo thứ tự tăng dần đơn đặt hàng theo ID tài liệu. Bạn có thể chỉ định thứ tự sắp xếp cho dữ liệu 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(), giá trị phải lớn hơn hoặc bằng về 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 bằng:

Web

import { query, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, orderBy("name"), limit(3));

Web

citiesRef.orderBy("name").limit(3);
Swift
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
citiesRef.order(by: "name").limit(to: 3)
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
[[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
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);
Python
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = query.get()

Python

cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = await query.get()
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();
Tiến hành
query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestore.Asc).LimitToLast(3)
PHP

PHP

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

$query = $citiesRef->orderBy('name')->limit(3);
Unity
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Query query = citiesRef.OrderBy("Name").Limit(3);
Ruby
query = cities_ref.order("name").limit(3)

Bạn cũng có thể sắp xếp theo thứ tự giảm dần để chọn 3 thành phố gần đây nhất:

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
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
[[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
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
Python
cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
Tiến hành
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP

PHP

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
Unity
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Query query = citiesRef.OrderByDescending("Name").Limit(3);
Ruby
query = cities_ref.order("name", "desc").limit(3)

Bạn cũng có thể sắp xếp theo nhiều trường. Ví dụ: nếu bạn muốn đặt hàng theo trạng thái và trong mỗi trạng thái theo thứ tự dân số theo thứ tự giảm dần:

Web

import { query, orderBy } from "firebase/firestore";  

const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));

Web

citiesRef.orderBy("state").orderBy("population", "desc");
Swift
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
citiesRef
  .order(by: "state")
  .order(by: "population", descending: true)
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
[[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
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
Python
cities_ref = db.collection("cities")
ordered_city_ref = cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)

Python

cities_ref = db.collection("cities")
cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
Tiến hành
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
PHP

PHP

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
Unity
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
Ruby
query = cities_ref.order("state").order("population", "desc")

Bạn có thể kết hợp bộ lọc where() với orderBy()limit(). Trong ví dụ sau: các truy vấn xác định ngưỡng dữ liệu, sắp xếp theo tổng 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

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
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
citiesRef
  .whereField("population", isGreaterThan: 100000)
  .order(by: "population")
  .limit(to: 2)
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
[[[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
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
Python
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
    .OrderBy("population")
    .Limit(2);
Node.js
const biggestRes = await citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2).get();
Tiến hành
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)
PHP

PHP

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

$query = $citiesRef
    ->where('population', '>', 2500000)
    ->orderBy('population')
    ->limit(2);
Unity
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
C#
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
Ruby
query = cities_ref.where("population", ">", 2_500_000).order("population").limit(2)

Tuy nhiên, nếu bạn có một bộ lọc có tính năng so sánh phạm vi (<, <=, >, >=), đơn đặt 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 orderBy() dưới đây.

Các điểm hạn chế

Xin lưu ý quy định hạn chế sau đây đối với mệnh đề orderBy():

orderBy và trạng thái tồn tại

Khi bạn sắp xếp một truy vấn theo một trường cho trước, truy vấn đó chỉ có thể trả về tài liệu có 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 trong đó Trường population không được đặt, ngay cả khi các trường đó đáp ứng bộ lọc truy vấn.

Java
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);

Hiệu ứng có liên quan áp dụng cho bất đẳng thức. Truy vấn có bộ lọc bất đẳng thức trên một trường cũng ngụ ý sắp xếp theo trường đó. Nội dung sau đây truy vấn không trả về tài liệu mà không có trường population nếu country = USA trong tài liệu đó . Để giải quyết sự cố này, bạn có thể thực thi các truy vấn riêng cho mỗi thứ tự hoặc bạn có thể chỉ định giá trị cho tất cả các trường mà bạn đặt hàng.

Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));

Truy vấn trên bao gồm một thứ tự ngụ ý theo bất đẳng thức và là tương đương với:

Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);