คอลเล็กชัน

คำอธิบาย

แสดงเอกสารทั้งหมดจากคอลเล็กชันที่ระบุ คอลเล็กชันสามารถซ้อนกันได้

ตัวอย่าง

Web

const results = await execute(db.pipeline()
  .collection("users/bob/games")
  .sort(field("name").ascending())
  );
Swift
let results = try await db.pipeline()
  .collection("users/bob/games")
  .sort([Field("name").ascending()])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("users/bob/games")
    .sort(field("name").ascending())
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
    .collection("users/bob/games")
    .sort(field("name").ascending())
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("users/bob/games")
    .sort(Field.of("name").ascending())
    .execute()
)
Java
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collection("users/bob/games")
        .sort(ascending(field("name")))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	Collection("users/bob/games").
	Sort(firestore.Orders(firestore.Ascending(firestore.FieldOf("name")))).
	Execute(ctx)

พฤติกรรม

หากต้องการใช้ระยะ collection(...) ระยะดังกล่าวต้องปรากฏเป็นระยะแรกในไปป์ไลน์ (หรือไปป์ไลน์ย่อย)

ลำดับของเอกสารที่แสดงผลจากระยะ collection(...) ไม่แน่นอนและไม่สามารถเชื่อถือได้ Firestore จะพยายามดำเนินการค้นหาด้วยวิธีที่มีประสิทธิภาพมากที่สุด ซึ่งอาจเปลี่ยนลำดับได้โดยขึ้นอยู่กับการกำหนดค่าสคีมาหรือดัชนี คุณสามารถใช้ระยะ sort(...) ที่ตามมาเพื่อรับการจัดลำดับเชิงกำหนดได้

ตัวอย่างเช่น สำหรับเอกสารต่อไปนี้

Node.js

await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"});
await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"});
await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"});
await db.collection("states").doc("CA").set({name: "California"});

คุณสามารถใช้ระยะ collection เพื่อดึงข้อมูลเมืองทั้งหมดในคอลเล็กชัน cities แล้วจัดเรียงตามลำดับชื่อจากน้อยไปมาก

Node.js

const results = await db.pipeline()
  .collection("/cities")
  .sort(field("name").ascending())
  .execute();

การค้นหานี้จะแสดงเอกสารต่อไปนี้

  { name: "Chicago", state: "Illinois" }
  { name: "New York City", state: "New York" }
  { name: "San Francisco", state: "California" }

คอลเล็กชันย่อย

นอกจากนี้ คุณยังใช้ระยะ collection(...) เพื่อกำหนดเป้าหมายคอลเล็กชันภายใต้ระดับบนที่เฉพาะเจาะจงได้โดยระบุเส้นทางแบบเต็มไปยังระยะนั้น

ตัวอย่างเช่น สำหรับเอกสารต่อไปนี้

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});

สำหรับตัวอย่างนี้ เราต้องการเฉพาะแผนกของนิวยอร์กซิตี้เท่านั้น

Node.js

const results = await db.pipeline()
  .collection("/cities/NY/departments")
  .sort(field("employees").ascending())
  .execute();

ซึ่งจะแสดงผลแผนกทั้งหมดภายใต้เส้นทางแบบเต็ม cities/NY/departments

  { name: "NY Building Deparment", employees: 1000 }
  { name: "NY Finance Deparment", employees: 1200 }