תיאור
מסננת את המסמכים מהשלב הקודם ומחזירה רק את המסמכים שבהם התנאי הוא true.
דוגמאות
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();
המשך
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 }
התנהגות
כמה שלבים
אפשר לשרשר כמה שלבים של where(...), כך שכל תנאי יפעל כביטוי של and(...).
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(field("location.country").equals("USA"))
.where(field("population").greaterThan(500000))
.execute();
סינון שמבוסס על or לוגי של שני תנאים צריך להתבצע בשלב where(...) אחד.
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(field("location.state").equals("NY").or(field("location.state").equals("CA")))
.execute();
ביטויים מורכבים
תנאי הסינון יכול להכיל תנאי סינון מורכבים שמכילים ביטויים מקוננים עמוקים ואופרטורים לוגיים. לדוגמה:
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 על סמך ביטוי רגולרי, או אם העיר נמצאת בUSA
עם שם מדינה ארוך מספיק. אפשר לציין כל ביטוי כתנאי, אבל הוא יתאים רק לביטויים ששווים ל-true.
הזמנת שלבים
סדר השלבים חשוב כי הוא יכול לשנות את סדר ההערכה של השאילתה. לדוגמה, השאילתה:
Node.js
const cities = await db.pipeline()
.collection("/cities")
.limit(10)
.where(field("location.country").equals("USA"))
.execute();
יסנן רק את location.country עבור קבוצה (אולי אקראית) של 10 מסמכים, כי השלב הקודם limit(...) מגביל את המסמכים שמועברים לשלב where(...). לכן, כלל האצבע הוא למקם את השלבים של where(...) מוקדם ככל האפשר בשאילתה.
HAVINGפונקציונליות דומה:
השלב where(...) יכול להופיע אחרי כל שלב שמשנה את הסכימה של המסמכים, כמו select(...) או aggregate(...), והוא יתייחס לשדות שנוצרו מהשלבים האלה. חשוב לציין שב-aggregate(...), פסקה של where(...) שמתייחסת לשדות המצטברים פועלת כמו פסקה של HAVING במערכת SQL רגילה. לדוגמה:
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))
מאפשרת להחזיר את המדינות שיש בהן ערים עם גודל אוכלוסייה כולל מעל ערך מסוים.