สถานที่

คำอธิบาย

กรองเอกสารจากขั้นตอนก่อนหน้า โดยแสดงเฉพาะเอกสาร ที่เงื่อนไขประเมินเป็น 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(...)หลายขั้นตอนสามารถเชื่อมโยงกันได้ โดยทำหน้าที่เป็นand(...) นิพจน์ในแต่ละเงื่อนไข

Node.js

const cities = await db.pipeline()
  .collection("/cities")
  .where(field("location.country").equals("USA"))
  .where(field("population").greaterThan(500000))
  .execute();

การกรองตามorเชิงตรรกะของ 2 เงื่อนไขต้องดำเนินการเป็น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))

ช่วยให้แสดงผลรัฐที่มีเมืองซึ่งมีประชากรเกินขนาดประชากรทั้งหมด