المكان

الوصف

تتم فلترة المستندات من المرحلة السابقة، ولا يتم عرض سوى المستندات التي يكون فيها الشرط 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))

تسمح بإرجاع الولايات التي تضم مدنًا يزيد إجمالي عدد سكانها عن حجم معين.