בפורמט זה

תיאור

מסנן את המסמכים מהשלב הקודם, ומחזיר רק את המסמכים שבהם התנאי מקבל את הערך 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(...), וזה שווה ערך מבחינה לוגית לשילוב של תנאי הסינון בשלב where(...) אחד באמצעות ביטוי and(...). שתי הגישות מפיקות את אותה תוכנית ביצוע, ואין להן השפעה על הביצוע או על הביצועים של השאילתה.

לדוגמה, שרשור של כמה שלבי where(...):

Node.js

const cities = await db.pipeline()
  .collection("/cities")
  .where(field("location.country").equals("USA"))
  .where(field("population").greaterThan(500000))
  .execute();

שווה ערך לשימוש בשלב where(...) יחיד עם and(...) מורכב:

Node.js

const cities = await db.pipeline()
  .collection("/cities")
  .where(
    field("location.country").equals("USA")
      .and(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))

מאפשרת להחזיר את המדינות שיש בהן ערים עם אוכלוסייה מעל גודל מסוים.