Bộ sưu tập

Mô tả

Trả về tất cả tài liệu trong một tập hợp nhất định. Bạn có thể lồng bộ sưu tập.

Cú pháp

Node.js

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

Ví dụ về ứng dụng

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()
)
Java
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collection("users/bob/games")
        .sort(ascending(field("name")))
        .execute()
        .get();

Hành vi

Để sử dụng giai đoạn collection, giai đoạn này phải xuất hiện dưới dạng giai đoạn đầu tiên trong quy trình.

Thứ tự của các tài liệu được trả về từ giai đoạn collection là không ổn định và bạn không nên dựa vào đó. Bạn có thể sử dụng giai đoạn sắp xếp tiếp theo để có được thứ tự xác định.

Ví dụ: đối với các tài liệu sau:

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

Bạn có thể dùng giai đoạn collection để truy xuất tất cả các thành phố trong tập hợp cities, sau đó sắp xếp các thành phố đó theo thứ tự tăng dần của tên.

Node.js

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

Truy vấn này tạo ra các tài liệu sau:

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

Bộ sưu tập con

Bạn cũng có thể dùng giai đoạn collection để nhắm đến các bộ sưu tập trong một thư mục mẹ cụ thể bằng cách cung cấp đường dẫn đầy đủ đến giai đoạn đó.

Ví dụ: đối với các tài liệu sau:

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

Trong ví dụ này, chúng ta chỉ muốn các phòng ban của thành phố New York.

Node.js

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

Thao tác này sẽ trả về tất cả các phòng ban theo đường dẫn đầy đủ cities/NY/departments.

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