集合群組 (輸入階段)

說明

傳回指定集合 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()
Java
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(...) 階段可用於從資料庫中所有上層集合的每個部門集合傳回文件。

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 }