कहां

ब्यौरा

यह फ़ंक्शन, पिछली स्टेज के दस्तावेज़ों को फ़िल्टर करता है. साथ ही, सिर्फ़ उन दस्तावेज़ों को दिखाता है जिनमें शर्त 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(...) स्टेज को एक साथ जोड़ा जा सकता है. यह लॉजिकली, and(...) एक्सप्रेशन का इस्तेमाल करके, एक ही where(...) स्टेज में फ़िल्टर की शर्तों को जोड़ने के बराबर होता है. दोनों तरीकों से एक ही एक्ज़ीक्यूशन प्लान बनता है. साथ ही, इनका क्वेरी एक्ज़ीक्यूशन या परफ़ॉर्मेंस पर कोई असर नहीं पड़ता.

उदाहरण के लिए, कई 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 पर फ़िल्टर करेगा. ऐसा इसलिए, क्योंकि 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))

इससे उन राज्यों की जानकारी मिलती है जिनमें शहरों की कुल आबादी ज़्यादा है.