설명
이전 단계의 문서를 필터링하여 조건이 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() )
자바
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();
두 조건의 논리적 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();
이전 limit(...) 단계에서 where(...) 단계에 제공되는 문서를 제한하므로 10개의 문서(무작위일 수 있음) 집합에 대해서만 location.country로 필터링됩니다. 따라서 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))
총 인구수가 일정 규모를 초과하는 도시가 있는 주를 반환할 수 있습니다.