Cơ sở dữ liệu

Mô tả

Trả về tất cả các tài liệu trong một cơ sở dữ liệu trên nhiều tập hợp và cấp độ lồng nhau.

Ví dụ

Web

// Count all documents in the database
const results = await execute(db.pipeline()
  .database()
  .aggregate(countAll().as("total"))
  );
Swift
// Count all documents in the database
let results = try await db.pipeline()
  .database()
  .aggregate([CountAll().as("total")])
  .execute()

Kotlin

// Count all documents in the database
val results = db.pipeline()
    .database()
    .aggregate(AggregateFunction.countAll().alias("total"))
    .execute()

Java

      // Count all documents in the database
Task<Pipeline.Snapshot> results = db.pipeline()
    .database()
    .aggregate(AggregateFunction.countAll().alias("total"))
    .execute();
    
Python
from google.cloud.firestore_v1.pipeline_expressions import Count

# Count all documents in the database
results = client.pipeline().database().aggregate(Count().as_("total")).execute()
Java
// Count all documents in the database
Pipeline.Snapshot results =
    firestore.pipeline().database().aggregate(countAll().as("total")).execute().get();
Bắt đầu
// Count all documents in the database
snapshot := client.Pipeline().
	Database().
	Aggregate(firestore.Accumulators(firestore.CountAll().As("total"))).
	Execute(ctx)

Hành vi

Để sử dụng giai đoạn database(...), 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 database(...) không ổn định và không thể dựa vào. Bạn có thể dùng giai đoạn sort(...) 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", population: 800000});
await db.collection("states").doc("CA").set({name: "California", population: 39000000});
await db.collection("countries").doc("USA").set({name: "United States of America", population: 340000000});

Bạn có thể dùng giai đoạn database(...) để truy xuất tất cả các tài liệu trong cơ sở dữ liệu.

Node.js

const results = await db.pipeline()
  .database()
  .sort(field("population").ascending())
  .execute();

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

  { name: "San Francsico", state: "California", population: 800000 }
  { name: "California", population: 39000000 }
  { name: "United States of America", population: 340000000 }