กลุ่มคอลเล็กชัน

คำอธิบาย

แสดงเอกสารทั้งหมดจากคอลเล็กชันใดก็ตามที่มีรหัสคอลเล็กชันที่ระบุ โดยไม่คำนึงถึงคอลเล็กชันระดับบน

ตัวอย่าง

Web

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

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();
    
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(...) ไม่แน่นอนและเชื่อถือไม่ได้ Cloud 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(...) เพื่อแสดงเอกสารจากคอลเล็กชัน departments ทุกรายการในคอลเล็กชันระดับบนทั้งหมดในฐานข้อมูล

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 }