कहां

ब्यौरा

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

Node.js

const cities = await db.pipeline()
  .collection("/cities")
  .where(field("location.country").equals("USA"))
  .where(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();

सिर्फ़ 10 दस्तावेज़ों के (संभावित तौर पर रैंडम) सेट के लिए, 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))

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