説明
前のステージのドキュメントをフィルタリングし、条件が true と評価されたドキュメントのみを返します。
構文:
where(condition: Expr)
例
次のドキュメントを含む cities コレクションを作成します。
Node.js
await db.collection('cities').doc('SF').set({name: 'San Francisco', state: 'CA', country: 'USA', population: 870000});
await db.collection('cities').doc('LA').set({name: 'Los Angeles', state: 'CA', country: 'USA', population: 3970000});
await db.collection('cities').doc('NY').set({name: 'New York', state: 'NY', country: 'USA', population: 8530000});
await db.collection('cities').doc('TOR').set({name: 'Toronto', state: null, country: 'Canada', population: 2930000});
await db.collection('cities').doc('MEX').set({name: 'Mexico City', state: null, country: 'Mexico', population: 9200000});
等価検索を実行します。
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(field("state").equals("CA"))
.execute();
次の結果を生成します。
{name: 'San Francisco', state: 'CA', country: 'USA', population: 870000},
{name: 'Los Angeles', state: 'CA', country: 'USA', population: 3970000}
クライアントの例
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();
動作
複数のステージ
複数の where(...) ステージを連結して、各条件で and(...) 式として機能させることができます。
Node.js
const cities = await db.pipeline()
.collection("/cities")
.where(field("location.country").equals("USA"))
.where(field("population").greaterThan(500000))
.execute();
ただし、2 つの条件の論理 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"))))
正規表現に基づくか、USA 内の都市で州名が十分に長い場合に、/cities をフィルタします。任意の式を条件として指定できますが、true と評価されるもののみが一致します。
ステージの順序
ステージの順序は、クエリの評価順序を変更する可能性があるため重要です。 たとえば、次のクエリです。
Node.js
const cities = await db.pipeline()
.collection("/cities")
.limit(10)
.where(field("location.country").equals("USA"))
.execute();
前の limit(...) ステージで where(...) ステージに提供されるドキュメントが制限されているため、location.country でフィルタされるのは、10 個の(ランダムである可能性がある)ドキュメントのセットのみです。このため、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))
一定の総人口規模を超える都市がある州を返します。