컬렉션 그룹 (입력 단계)
설명
상위 요소에 관계없이 지정된 컬렉션 ID가 있는 모든 컬렉션의 모든 문서를 반환합니다.
예시
웹
const results = await execute(db.pipeline() .collectionGroup("games") .sort(field("name").ascending()) );
Swift
let results = try await db.pipeline() .collectionGroup("games") .sort([Field("name").ascending()]) .execute()
Kotlin
Android
val results = db.pipeline() .collectionGroup("games") .sort(field("name").ascending()) .execute()
자바
Android
Task<Pipeline.Snapshot> results = db.pipeline() .collectionGroup("games") .sort(field("name").ascending()) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field results = ( client.pipeline() .collection_group("games") .sort(Field.of("name").ascending()) .execute() )
Java
Pipeline.Snapshot results = firestore .pipeline() .collectionGroup("games") .sort(ascending(field("name"))) .execute() .get();
Go
snapshot := client.Pipeline(). CollectionGroup("games"). Sort(firestore.Orders(firestore.Ascending(firestore.FieldOf("name")))). Execute(ctx)
동작
collection_group(...) 단계를 사용하려면 파이프라인의 첫 번째 단계로 표시되어야 합니다.
collection_group(...) 단계에서 반환되는 문서의 순서는 불안정하므로 이에 의존해서는 안 됩니다. Firestore는 가능한 가장 효율적인 방식으로 쿼리를 실행하려고 시도하며, 스키마 또는 색인 구성에 따라 순서가 변경될 수 있습니다. 후속 sort(...) 단계를 사용하여 결정적 순서를 가져올 수 있습니다.
예를 들어 다음 문서의 경우:
Node.js
await db.collection("cities/SF/departments").doc("building").set({name: "SF Building Deparment", employees: 750});
await db.collection("cities/NY/departments").doc("building").set({name: "NY Building Deparment", employees: 1000});
await db.collection("cities/CHI/departments").doc("building").set({name: "CHI Building Deparment", employees: 900});
await db.collection("cities/NY/departments").doc("finance").set({name: "NY Finance Deparment", employees: 1200});
collection_group(...) 단계를 사용하여 데이터베이스의 모든 상위 컬렉션에 있는 모든 departments 컬렉션에서 문서를 반환할 수 있습니다.
Node.js
const results = await db.pipeline()
.collectionGroup("departments")
.sort(field("employees").ascending())
.execute();
이 쿼리는 다음과 같은 문서를 생성합니다.
{ name: "SF Building Deparment", employees: 750 }
{ name: "CHI Building Deparment", employees: 900 }
{ name: "NY Building Deparment", employees: 1000 }
{ name: "NY Finance Deparment", employees: 1200 }