מסמכים

תיאור

הפונקציה מחזירה מסמכים על ידי חיפוש בקבוצה קבועה של מסמכים מוגדרים מראש.

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

השלב הזה פועל באופן דומה ל-batchGet של Cloud Firestore, ומאפשר לסנן את התוצאות ישירות במקום לבצע שלבי סינון משני אחרי פעולת האצווה.

דוגמאות

Web

const results = await execute(db.pipeline()
  .documents([
    doc(db, "cities", "SF"),
    doc(db, "cities", "DC"),
    doc(db, "cities", "NY")
  ])
);
Swift
let results = try await db.pipeline()
  .documents([
    db.collection("cities").document("SF"),
    db.collection("cities").document("DC"),
    db.collection("cities").document("NY")
  ]).execute()

Kotlin

val results = db.pipeline()
    .documents(
        db.collection("cities").document("SF"),
        db.collection("cities").document("DC"),
        db.collection("cities").document("NY")
    ).execute()

Java

      Task<Pipeline.Snapshot> results = db.pipeline()
    .documents(
        db.collection("cities").document("SF"),
        db.collection("cities").document("DC"),
        db.collection("cities").document("NY")
    ).execute();
    
Python
results = (
    client.pipeline()
    .documents(
        client.collection("cities").document("SF"),
        client.collection("cities").document("DC"),
        client.collection("cities").document("NY"),
    )
    .execute()
)
Java
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .documents(
            firestore.collection("cities").document("SF"),
            firestore.collection("cities").document("DC"),
            firestore.collection("cities").document("NY"))
        .execute()
        .get();
המשך
snapshot := client.Pipeline().
	Documents([]*firestore.DocumentRef{
		client.Collection("cities").Doc("SF"),
		client.Collection("cities").Doc("DC"),
		client.Collection("cities").Doc("NY"),
	}).
	Execute(ctx)

התנהגות

כדי להשתמש בשלב documents(...), הוא חייב להופיע כשלב הראשון בצינור עיבוד הנתונים.

סדר המסמכים שמוחזרים מהשלב documents(...) לא יציב ואי אפשר להסתמך עליו. אפשר להשתמש בשלב 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"});

אפשר להשתמש בשלב documents(...) כדי לאחזר רק את המסמכים SF ו-NYC, ואז למיין אותם בסדר עולה לפי השם.

Node.js

const results = await db.pipeline()
  .documents(
    db.collection("cities").doc("SF"),
    db.collection("cities").doc("NYC"))
  .sort(field("name").ascending())
  .execute();

השאילתה הזו יוצרת את המסמכים הבאים:

  { name: "New York City", state: "New York" }
  { name: "San Francsico", state: "California" }