কোথায়

বর্ণনা

পূর্ববর্তী ধাপের ডকুমেন্টগুলো ফিল্টার করে শুধুমাত্র সেই ডকুমেন্টগুলো ফেরত দেয়, যেখানে শর্তটি 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();

যেহেতু পূর্ববর্তী limit(...) পর্যায়টি where(...) পর্যায়ে সরবরাহ করা ডকুমেন্টগুলোকে সীমাবদ্ধ করে দিচ্ছে, তাই এটি শুধুমাত্র location.country এর উপর ভিত্তি করে (সম্ভাব্য র‍্যান্ডম) ১০টি ডকুমেন্টের একটি সেট ফিল্টার করবে। এই কারণে, সাধারণ নিয়ম হলো where(...) পর্যায়গুলোকে কোয়েরির যত আগে সম্ভব রাখা।

HAVING -সদৃশ কার্যকারিতা:

where(...) ` ধাপটি select(...) বা aggregate(...) এর মতো ডকুমেন্টের স্কিমা পরিবর্তনকারী যেকোনো ধাপের পরে আসতে পারে এবং এটি ঐ ধাপগুলো থেকে তৈরি হওয়া ফিল্ডগুলোকে নির্দেশ করবে। ` aggregate(...) -এর জন্য গুরুত্বপূর্ণ বিষয় হলো, পরবর্তী ` where(...) ` ক্লজ যা জমা হওয়া ফিল্ডগুলোকে নির্দেশ করে, তা একটি সাধারণ SQL সিস্টেমের HAVING ক্লজের মতো কাজ করে। উদাহরণস্বরূপ:

নোড.জেএস

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

যেসব রাজ্যে একটি নির্দিষ্ট মোট জনসংখ্যার চেয়ে বেশি জনসংখ্যার শহর রয়েছে, সেগুলোকে ফেরত দেওয়ার সুযোগ দেয়।