جایی که

توضیحات

اسناد مرحله قبل را فیلتر می‌کند و فقط اسنادی را برمی‌گرداند که در آنها شرط به 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)))
);
سویفت
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();
پایتون
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()
)
جاوا
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(...) برای هر شرط عمل کنند.

نود جی اس

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

فیلتر کردن بر اساس یک or دو شرط منطقی، باید به صورت یک مرحله‌ی where(...) انجام شود.

نود جی اس

const cities = await db.pipeline()
  .collection("/cities")
  .where(field("location.state").equals("NY").or(field("location.state").equals("CA")))
  .execute();

عبارات پیچیده

شرط فیلتر می‌تواند شامل شرط‌های فیلتر پیچیده‌ای باشد که شامل عبارات تو در تو و عملگرهای منطقی باشند. برای مثال:

نود جی اس

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 ارزیابی می‌شوند، مطابقت خواهد داشت.

سفارش مرحله‌ای

ترتیب مراحل مهم است زیرا می‌تواند ترتیب ارزیابی پرس‌وجو را تغییر دهد. برای مثال، پرس‌وجوی زیر:

نود جی اس

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

فقط location.country را برای مجموعه‌ای (احتمالاً تصادفی) از 10 سند فیلتر می‌کند، زیرا مرحله‌ی prior limit(...) اسنادی را که تا به حال به مرحله‌ی where(...) ارائه شده‌اند، محدود می‌کند. با توجه به این موضوع، قاعده‌ی کلی این است که مراحل where(...) را تا حد امکان در اوایل پرس‌وجو قرار دهید.

HAVING عملکردی مشابه:

مرحله‌ی where(...) می‌تواند بعد از هر مرحله‌ای که طرح اسناد را تغییر می‌دهد، مانند select(...) یا aggregate(...) بیاید و به فیلدهای تولید شده از آن مراحل اشاره خواهد کرد. نکته‌ی مهم برای aggregate(...) ، یک عبارت where(...) که به فیلدهای انباشته شده اشاره می‌کند، مانند یک عبارت HAVING در یک سیستم SQL معمولی عمل می‌کند. به عنوان مثال:

نود جی اس

const cities = await db.pipeline()
  .collection("/cities")
  .aggregate({
    accumulators: [field("population").sum().as("total_population")],
    groups: ["location.state"]
  })
  .where(field("total_population").greaterThan(10000000))

امکان بازگرداندن ایالت‌هایی را فراهم می‌کند که شهرهایی با جمعیت بیش از یک جمعیت کل دارند.