বিবরণ
আগের ধাপ থেকে ডকুমেন্ট ফিল্টার করে, শুধুমাত্র সেইসব ডকুমেন্ট দেখায়
যেখানে শর্ত 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() )
জাভা
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();
Go
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(...) এক্সপ্রেশন ব্যবহার করে একটি where(...) স্টেজের মধ্যে
ফিল্টার কন্ডিশন একত্রিত করার সমতুল্য। দুটি পদ্ধতিই একই এক্সিকিউশন প্ল্যান তৈরি করে
এবং কোয়েরি এক্সিকিউশন বা পারফর্ম্যান্সে কোনও প্রভাব ফেলে না।
যেমন, একাধিক where(...) স্টেজ একসাথে করা:
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(field("location.country").equals("USA"))
.where(field("population").greaterThan(500000))
.execute();
and(...) কম্পাউন্ডের সাথে একটি where(...) স্টেজ ব্যবহার করার সমতুল্য:
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-এ ফিল্টার করবে, কারণ পূর্ববর্তী limit(...) পর্যায়টি
where(...) পর্যায়ে কখনও প্রদান করা ডকুমেন্টগুলিকে সীমাবদ্ধ করছে। এই কারণে, কোয়েরির যত আগে সম্ভব where(...) স্টেজ যোগ করা হল
থাম্ব রুল।
HAVING-এর মতো কার্যকারিতা:
where(...) ধাপটি এমন যেকোনও ধাপের পরে আসতে পারে যা ডকুমেন্টের স্কিমা পরিবর্তন করে, যেমন select(...) বা aggregate(...) এবং সেইসব ধাপ থেকে তৈরি হওয়া ফিল্ডকে রেফার করবে। aggregate(...)-এর জন্য গুরুত্বপূর্ণ হল,
সঞ্চয় করা ফিল্ডকে রেফার করে এমন একটি where(...) ক্লজ,
সাধারণ SQL সিস্টেমে HAVING ক্লজের মতো কাজ করে। যেমন:
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))
মোট জনসংখ্যার আকারের চেয়ে বেশি শহর আছে এমন রাজ্যগুলি ফেরত দেওয়ার অনুমতি দেয়।