컬렉션

설명

지정된 컬렉션의 모든 문서를 반환합니다. 컬렉션은 중첩될 수 있습니다.

구문

Node.js

const results = await db.pipeline()
  .collection('/cities/SF/departments')
  .execute();

클라이언트 예시

Web

const results = await execute(db.pipeline()
  .collection("users/bob/games")
  .sort(field("name").ascending())
  );
Swift
let results = try await db.pipeline()
  .collection("users/bob/games")
  .sort([Field("name").ascending()])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("users/bob/games")
    .sort(field("name").ascending())
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
    .collection("users/bob/games")
    .sort(field("name").ascending())
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("users/bob/games")
    .sort(Field.of("name").ascending())
    .execute()
)
자바
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collection("users/bob/games")
        .sort(ascending(field("name")))
        .execute()
        .get();

동작

collection 단계를 사용하려면 파이프라인의 첫 번째 단계로 표시되어야 합니다.

collection 단계에서 반환되는 문서의 순서는 불안정하므로 이에 의존해서는 안 됩니다. 후속 정렬 단계를 사용하여 결정적 순서를 가져올 수 있습니다.

예를 들어 다음 문서의 경우:

Node.js

await db.collection('cities').doc('SF').set({name: 'San Francsico', state: 'California'});
await db.collection('cities').doc('NYC').set({name: 'New York City', state: 'New York'});
await db.collection('cities').doc('CHI').set({name: 'Chicago', state: 'Illinois'});
await db.collection('states').doc('CA').set({name: 'California'});

collection 단계를 사용하여 cities 컬렉션의 모든 도시를 검색한 다음 이름의 오름차순으로 정렬할 수 있습니다.

Node.js

const results = await db.pipeline()
  .collection('/cities')
  .sort(field('name').ascending())
  .execute();

이 쿼리는 다음과 같은 문서를 생성합니다.

  {name: 'Chicago', state: 'Illinois'}
  {name: 'New York City', state: 'New York'}
  {name: 'San Francisco', state: 'California'}

하위 컬렉션

collection 단계는 단계의 전체 경로를 제공하여 특정 상위 항목 아래의 컬렉션을 타겟팅하는 데에도 사용할 수 있습니다.

예를 들어 다음 문서의 경우:

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});

이 예에서는 뉴욕시의 부서만 필요합니다.

Node.js

const results = await db.pipeline()
  .collection('/cities/NY/departments')
  .sort(field('employees').ascending())
  .execute();

이렇게 하면 전체 경로 cities/NY/departments 아래의 모든 부서가 반환됩니다.

  {name: 'NY Building Deparment', employees: 1000}
  {name: 'NY Finance Deparment', employees: 1200}