คำอธิบาย
กรองเอกสารจากขั้นตอนก่อนหน้า โดยแสดงเฉพาะเอกสารที่เงื่อนไขประเมินเป็น 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();
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(...) หลายขั้นตอนสามารถเชื่อมโยงกันได้ ซึ่งเทียบเท่ากับการรวมเงื่อนไขตัวกรองภายในwhere(...) ขั้นตอนเดียว
โดยใช้นิพจน์ and(...) ทั้ง 2 วิธีจะสร้างแผนการดำเนินการเดียวกัน
และไม่มีผลต่อการดำเนินการหรือประสิทธิภาพของคําค้นหา
เช่น การเชื่อมโยง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))
อนุญาตให้แสดงรัฐที่มีเมืองซึ่งมีประชากรเกินขนาดประชากรทั้งหมด