گروه جمع‌آوری

توضیحات

تمام اسناد را از هر مجموعه ای که دارای شناسه مجموعه مشخص شده باشد، صرف نظر از والد آن، برمی‌گرداند.

مثال‌ها

Web

const results = await execute(db.pipeline()
  .collectionGroup("games")
  .sort(field("name").ascending())
  );
سویفت
let results = try await db.pipeline()
  .collectionGroup("games")
  .sort([Field("name").ascending()])
  .execute()

Kotlin

val results = db.pipeline()
    .collectionGroup("games")
    .sort(field("name").ascending())
    .execute()

Java

      Task<Pipeline.Snapshot> results = db.pipeline()
    .collectionGroup("games")
    .sort(field("name").ascending())
    .execute();
    
پایتون
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection_group("games")
    .sort(Field.of("name").ascending())
    .execute()
)
جاوا
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collectionGroup("games")
        .sort(ascending(field("name")))
        .execute()
        .get();
برو
snapshot := client.Pipeline().
	CollectionGroup("games").
	Sort(firestore.Orders(firestore.Ascending(firestore.FieldOf("name")))).
	Execute(ctx)

رفتار

برای استفاده از مرحله collection_group(...) ، باید به عنوان اولین مرحله در خط لوله ظاهر شود.

ترتیب اسناد برگردانده شده از مرحله collection_group(...) ناپایدار است و نمی‌توان به آن اعتماد کرد. Cloud Firestore تلاش خواهد کرد تا پرس‌وجو را به کارآمدترین روش ممکن اجرا کند، که می‌تواند ترتیب را بسته به طرحواره یا پیکربندی شاخص تغییر دهد. مرحله بعدی sort(...) می‌تواند برای به دست آوردن یک ترتیب قطعی استفاده شود.

به عنوان مثال، برای اسناد زیر:

نود جی اس

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(...) می‌تواند برای بازگرداندن اسناد از هر مجموعه department در تمام مجموعه‌های والد در پایگاه داده استفاده شود.

نود جی اس

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 }