קבוצת אוספים (שלב קלט)

תיאור

הפונקציה מחזירה את כל המסמכים מכל אוסף עם מזהה האוסף שצוין, ללא קשר לאוסף האב שלו.

דוגמאות

אינטרנט

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();
המשך
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 }