說明
篩選前一階段的文件,只傳回條件評估為 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(...) 運算式,在單一 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();
只會篩選 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))
可傳回城市總人口數超過特定值的州。