مجموعه

توضیحات

تمام اسناد را از یک مجموعه داده شده برمی‌گرداند. این مجموعه می‌تواند تو در تو باشد.

مثال‌ها

Web

const results = await execute(db.pipeline()
  .collection("users/bob/games")
  .sort(field("name").ascending())
  );
سویفت
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();
پایتون
from google.cloud.firestore_v1.pipeline_expressions import Field

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

رفتار

برای استفاده از مرحله collection(...) ، باید به عنوان اولین مرحله در خط لوله (یا زیرخط لوله) ظاهر شود.

ترتیب اسناد برگردانده شده از مرحله collection(...) ناپایدار است و نمی‌توان به آن اعتماد کرد. Firestore تلاش می‌کند تا پرس‌وجو را به کارآمدترین روش ممکن اجرا کند، که می‌تواند ترتیب را بسته به طرحواره یا پیکربندی شاخص تغییر دهد. مرحله sort(...) بعدی می‌تواند برای به دست آوردن یک ترتیب قطعی استفاده شود.

به عنوان مثال، برای اسناد زیر:

نود جی اس

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 و سپس مرتب‌سازی آنها به ترتیب صعودی نام استفاده شود.

نود جی اس

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(...) همچنین می‌تواند با ارائه مسیر کامل به مرحله، برای هدف قرار دادن مجموعه‌های تحت یک والد خاص مورد استفاده قرار گیرد.

به عنوان مثال، برای اسناد زیر:

نود جی اس

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

برای این مثال، ما فقط بخش‌های شهر نیویورک را می‌خواهیم.

نود جی اس

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 }