コレクション グループ(入力ステージ)
説明
親に関係なく、指定されたコレクション 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 }