איסוף

תיאור

מחזירה את כל המסמכים מאוסף נתון. האוסף יכול להיות מקונן.

דוגמאות

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

התנהגות

כדי להשתמש בשלב 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 }