Mô tả
Lọc các tài liệu từ giai đoạn trước, chỉ trả về những tài liệu mà điều kiện đánh giá là true.
Ví dụ
Web
let results; results = await execute(db.pipeline().collection("books") .where(field("rating").equal(5)) .where(field("published").lessThan(1900)) ); results = await execute(db.pipeline().collection("books") .where(and(field("rating").equal(5), field("published").lessThan(1900))) );
Swift
var results: Pipeline.Snapshot results = try await db.pipeline().collection("books") .where(Field("rating").equal(5)) .where(Field("published").lessThan(1900)) .execute() results = try await db.pipeline().collection("books") .where(Field("rating").equal(5) && Field("published").lessThan(1900)) .execute()
Kotlin
var results: Task<Pipeline.Snapshot> results = db.pipeline().collection("books") .where(field("rating").equal(5)) .where(field("published").lessThan(1900)) .execute() results = db.pipeline().collection("books") .where(Expression.and(field("rating").equal(5), field("published").lessThan(1900))) .execute()
Java
Task<Pipeline.Snapshot> results; results = db.pipeline().collection("books") .where(field("rating").equal(5)) .where(field("published").lessThan(1900)) .execute(); results = db.pipeline().collection("books") .where(Expression.and( field("rating").equal(5), field("published").lessThan(1900) )) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import And, Field results = ( client.pipeline() .collection("books") .where(Field.of("rating").equal(5)) .where(Field.of("published").less_than(1900)) .execute() ) results = ( client.pipeline() .collection("books") .where(And(Field.of("rating").equal(5), Field.of("published").less_than(1900))) .execute() )
Java
Pipeline.Snapshot results1 = firestore .pipeline() .collection("books") .where(field("rating").equal(5)) .where(field("published").lessThan(1900)) .execute() .get(); Pipeline.Snapshot results2 = firestore .pipeline() .collection("books") .where(and(field("rating").equal(5), field("published").lessThan(1900))) .execute() .get();
Bắt đầu
results1, err := client.Pipeline(). Collection("books"). Where(firestore.FieldOf("rating").Equal(5)). Where(firestore.FieldOf("published").LessThan(1900)). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err } results2, err := client.Pipeline(). Collection("books"). Where(firestore.And( firestore.FieldOf("rating").Equal(5), firestore.FieldOf("published").LessThan(1900), )). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err }
Hành vi
Nhiều giai đoạn
Bạn có thể liên kết nhiều giai đoạn where(...) với nhau, đóng vai trò là một biểu thức and(...) trên mỗi điều kiện.
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(field("location.country").equals("USA"))
.where(field("population").greaterThan(500000))
.execute();
Lọc dựa trên or logic của hai điều kiện mặc dù cần được thực hiện dưới dạng một giai đoạn where(...) duy nhất.
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(field("location.state").equals("NY").or(field("location.state").equals("CA")))
.execute();
Biểu thức phức tạp
Điều kiện lọc có thể chứa các điều kiện lọc phức tạp chứa các biểu thức lồng nhau sâu và toán tử logic. Ví dụ:
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(
field("name").like("San%")
.or(
field("location.state").charLength().greaterThan(7)
.and(field("location.country").equals("USA"))))
/cities lọc dựa trên biểu thức chính quy hoặc nếu thành phố nằm trong USA có tên tiểu bang đủ dài. Bạn có thể đưa ra bất kỳ biểu thức nào làm điều kiện, nhưng chỉ những biểu thức nào được đánh giá là true mới khớp.
Sắp xếp giai đoạn
Thứ tự của các giai đoạn rất quan trọng vì có thể thay đổi thứ tự đánh giá truy vấn. Ví dụ: cụm từ tìm kiếm
Node.js
const cities = await db.pipeline()
.collection("/cities")
.limit(10)
.where(field("location.country").equals("USA"))
.execute();
sẽ chỉ lọc trên location.country cho một nhóm 10 tài liệu (có thể ngẫu nhiên) vì giai đoạn limit(...) trước đó đang hạn chế những tài liệu từng được cung cấp cho giai đoạn where(...). Do đó, quy tắc chung là đặt các giai đoạn where(...) càng sớm càng tốt trong truy vấn.
Chức năng tương tự như HAVING:
Giai đoạn where(...) có thể diễn ra sau bất kỳ giai đoạn nào thay đổi giản đồ của tài liệu, chẳng hạn như select(...) hoặc aggregate(...) và sẽ tham chiếu đến các trường được tạo ra từ những giai đoạn đó. Điều quan trọng đối với aggregate(...) là mệnh đề where(...) sau đây đề cập đến các trường tích luỹ hoạt động như một mệnh đề HAVING trong hệ thống SQL thông thường. Ví dụ:
Node.js
const cities = await db.pipeline()
.collection("/cities")
.aggregate({
accumulators: [field("population").sum().as("total_population")],
groups: ["location.state"]
})
.where(field("total_population").greaterThan(10000000))
cho phép trả về các tiểu bang có thành phố có tổng quy mô dân số.