מודל נתונים

‫Firestore הוא מסד נתונים מבוסס-מסמכים, NoSQL. בניגוד למסד נתונים של SQL, אין טבלאות או שורות. במקום זאת, הנתונים מאוחסנים במסמכים שמאורגנים באוספים.

כל מסמך מכיל קבוצה של צמדי מפתח/ערך. ‫Firestore מותאם לאחסון אוספים גדולים של מסמכים קטנים.

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

אוספים ומסמכים נוצרים באופן משתמע ב-Firestore. פשוט מקצים נתונים למסמך בתוך אוסף. אם האוסף או המסמך לא קיימים, Firestore יוצר אותם.

מסמכים

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

מסמך שמייצג משתמש alovelace יכול להיראות כך:

  • alovelace

    first : "Ada"
    last : "Lovelace"
    born : 1815

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

  • alovelace

    name :
        first : "Ada"
        last : "Lovelace"
    born : 1815

יכול להיות שתשימו לב שהמסמכים דומים מאוד ל-JSON. למעשה, הם כמעט זהים. יש כמה הבדלים (לדוגמה, מסמכים תומכים בסוגי נתונים נוספים ומוגבלים לגודל המסמך המקסימלי), אבל באופן כללי אפשר להתייחס למסמכים כאל רשומות JSON קלות.

אוספים

מסמכים נמצאים באוספים, שהם מאגרים של מסמכים. לדוגמה, יכול להיות אוסף users שיכיל את המשתמשים השונים, כשכל אחד מהם מיוצג על ידי מסמך:

  • משתמשים

    • alovelace

      first : "Ada"
      last : "Lovelace"
      born : 1815

    • ‫ aturing

      first : "Alan"
      last : "Turing"
      born : 1912

ל-Firestore אין סכימה, כך שאתם יכולים לבחור אילו שדות להוסיף לכל מסמך ואילו סוגי נתונים לאחסן בשדות האלה. מסמכים באותו אוסף יכולים להכיל שדות שונים או לאחסן סוגי נתונים שונים בשדות האלה. עם זאת, מומלץ להשתמש באותם שדות וסוגי נתונים בכמה מסמכים, כדי שתוכלו לשלוח שאילתות למסמכים בקלות רבה יותר.

אוסף מכיל מסמכים בלבד. הוא לא יכול להכיל ישירות שדות גולמיים עם ערכים, והוא לא יכול להכיל אוספים אחרים. (במאמר נתונים היררכיים מוסבר איך לבנות נתונים מורכבים יותר ב-Firestore).

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

אין צורך "ליצור" או "למחוק" אוספים. אחרי שיוצרים את המסמך הראשון באוסף, האוסף קיים. אם מוחקים את כל המסמכים באוסף, הוא מפסיק להתקיים.

קובצי עזר

כל מסמך ב-Firestore מזוהה באופן ייחודי לפי המיקום שלו במסד הנתונים. בדוגמה הקודמת מוצג מסמך alovelace בתוך האוסף users. כדי להתייחס למיקום הזה בקוד, אפשר ליצור הפניה אליו.

גרסה 9 לאינטרנט

import { doc } from "firebase/firestore";

const alovelaceDocumentRef = doc(db, 'users', 'alovelace');

גרסה 8 לאינטרנט

var alovelaceDocumentRef = db.collection('users').doc('alovelace');
Swift
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
let alovelaceDocumentRef = db.collection("users").document("alovelace")
Objective-C
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
FIRDocumentReference *alovelaceDocumentRef =
    [[self.db collectionWithPath:@"users"] documentWithPath:@"alovelace"];
Kotlin
Android
val alovelaceDocumentRef = db.collection("users").document("alovelace")
Java
Android
DocumentReference alovelaceDocumentRef = db.collection("users").document("alovelace");

Dart

final alovelaceDocumentRef = db.collection("users").doc("alovelace");
Java
// Reference to a document with id "alovelace" in the collection "users"
DocumentReference document = db.collection("users").document("alovelace");
Python
a_lovelace_ref = db.collection("users").document("alovelace")
Python
(Async)
a_lovelace_ref = db.collection("users").document("alovelace")
C++‎
DocumentReference alovelace_document_reference =
    db->Collection("users").Document("alovelace");
Node.js
const alovelaceDocumentRef = db.collection('users').doc('alovelace');
המשך

import (
	"cloud.google.com/go/firestore"
)

func createDocReference(client *firestore.Client) {

	alovelaceRef := client.Collection("users").Doc("alovelace")

	_ = alovelaceRef
}
PHP

PHP

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

$document = $db->collection('samples/php/users')->document('alovelace');
Unity
DocumentReference documentRef = db.Collection("users").Document("alovelace");
C#‎

C#

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

DocumentReference documentRef = db.Collection("users").Document("alovelace");
Ruby
document_ref = firestore.col("users").doc("alovelace")

הפניה היא אובייקט קל משקל שרק מצביע על מיקום במסד הנתונים. אפשר ליצור הפניה גם אם אין שם נתונים, ויצירת הפניה לא מבצעת פעולות ברשת.

אפשר גם ליצור הפניות לאוספים:

גרסה 9 לאינטרנט

import { collection } from "firebase/firestore";

const usersCollectionRef = collection(db, 'users');

גרסה 8 לאינטרנט

var usersCollectionRef = db.collection('users');
Swift
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
let usersCollectionRef = db.collection("users")
Objective-C
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
FIRCollectionReference *usersCollectionRef = [self.db collectionWithPath:@"users"];
Kotlin
Android
val usersCollectionRef = db.collection("users")
Java
Android
CollectionReference usersCollectionRef = db.collection("users");

Dart

final usersCollectionRef = db.collection("users");
Java
// Reference to the collection "users"
CollectionReference collection = db.collection("users");
Python
users_ref = db.collection("users")
Python
(Async)
users_ref = db.collection("users")
C++‎
CollectionReference users_collection_reference = db->Collection("users");
Node.js
const usersCollectionRef = db.collection('users');
המשך

import (
	"cloud.google.com/go/firestore"
)

func createCollectionReference(client *firestore.Client) {
	usersRef := client.Collection("users")

	_ = usersRef
}
PHP

PHP

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

$collection = $db->collection('samples/php/users');
Unity
CollectionReference collectionRef = db.Collection("users");
C#‎

C#

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

CollectionReference collectionRef = db.Collection("users");
Ruby
collection_ref = firestore.col "users"

כדי ליצור הפניות, אפשר גם לציין את הנתיב למסמך או לאוסף כמחרוזת, עם רכיבי נתיב שמופרדים בלוכסן (/). לדוגמה, כדי ליצור הפניה למסמך alovelace:

גרסה 9 לאינטרנט

import { doc } from "firebase/firestore"; 

const alovelaceDocumentRef = doc(db, 'users/alovelace');

גרסה 8 לאינטרנט

var alovelaceDocumentRef = db.doc('users/alovelace');
Swift
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
let aLovelaceDocumentReference = db.document("users/alovelace")
Objective-C
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
FIRDocumentReference *aLovelaceDocumentReference =
    [self.db documentWithPath:@"users/alovelace"];
Kotlin
Android
val alovelaceDocumentRef = db.document("users/alovelace")
Java
Android
DocumentReference alovelaceDocumentRef = db.document("users/alovelace");

Dart

final aLovelaceDocRef = db.doc("users/alovelace");
Java
// Reference to a document with id "alovelace" in the collection "users"
DocumentReference document = db.document("users/alovelace");
Python
a_lovelace_ref = db.document("users/alovelace")
Python
(Async)
a_lovelace_ref = db.document("users/alovelace")
C++‎
DocumentReference alovelace_document = db->Document("users/alovelace");
Node.js
const alovelaceDocumentRef = db.doc('users/alovelace');
המשך

import (
	"cloud.google.com/go/firestore"
)

func createDocReferenceFromString(client *firestore.Client) {
	// Reference to a document with id "alovelace" in the collection "users"
	alovelaceRef := client.Doc("users/alovelace")

	_ = alovelaceRef
}
PHP

PHP

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

$document = $db->document('users/alovelace');
Unity
DocumentReference documentRef = db.Document("users/alovelace");
C#‎

C#

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

DocumentReference documentRef = db.Document("users/alovelace");
Ruby
document_path_ref = firestore.doc "users/alovelace"

נתונים היררכיים

כדי להבין איך פועלים מבני נתונים היררכיים ב-Firestore, נבחן דוגמה של אפליקציית צ'אט עם הודעות וחדרי צ'אט.

אתם יכולים ליצור אוסף בשם rooms כדי לאחסן חדרי צ'אט שונים:

  • חדרים

    • חדרים

      name : "my chat room"

    • roomB

      ...

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

אוספי משנה

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

אפשר ליצור אוסף משנה בשם messages לכל מסמך של חדר באוסף rooms:

  • חדרים

    • חדרים

      name : "my chat room"

      • הודעות

        • message1

          from : "alex"
          msg : "Hello World!"

        • message2

          ...

    • roomB

      ...

בדוגמה הזו, אתם יכולים ליצור הפניה להודעה באוסף המשנה באמצעות הקוד הבא:

גרסה 9 לאינטרנט

import { doc } from "firebase/firestore"; 

const messageRef = doc(db, "rooms", "roomA", "messages", "message1");

גרסה 8 לאינטרנט

var messageRef = db.collection('rooms').doc('roomA')
                .collection('messages').doc('message1');
Swift
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
let messageRef = db
  .collection("rooms").document("roomA")
  .collection("messages").document("message1")
Objective-C
הערה: המוצר הזה לא זמין ביעדים של watchOS ו-App Clip.
FIRDocumentReference *messageRef =
    [[[[self.db collectionWithPath:@"rooms"] documentWithPath:@"roomA"]
    collectionWithPath:@"messages"] documentWithPath:@"message1"];
Kotlin
Android
val messageRef = db
    .collection("rooms").document("roomA")
    .collection("messages").document("message1")
Java
Android
DocumentReference messageRef = db
        .collection("rooms").document("roomA")
        .collection("messages").document("message1");

Dart

final messageRef = db
    .collection("rooms")
    .doc("roomA")
    .collection("messages")
    .doc("message1");
Java
// Reference to a document in subcollection "messages"
DocumentReference document =
    db.collection("rooms").document("roomA").collection("messages").document("message1");
Python
room_a_ref = db.collection("rooms").document("roomA")
message_ref = room_a_ref.collection("messages").document("message1")
Python
(Async)
room_a_ref = db.collection("rooms").document("roomA")
message_ref = room_a_ref.collection("messages").document("message1")
C++‎
DocumentReference message_reference = db->Collection("rooms")
    .Document("roomA")
    .Collection("messages")
    .Document("message1");
Node.js
const messageRef = db.collection('rooms').doc('roomA')
  .collection('messages').doc('message1');
המשך

import (
	"cloud.google.com/go/firestore"
)

func createSubcollectionReference(client *firestore.Client) {
	messageRef := client.Collection("rooms").Doc("roomA").
		Collection("messages").Doc("message1")

	_ = messageRef
}
PHP

PHP

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

$document = $db
    ->collection('rooms')
    ->document('roomA')
    ->collection('messages')
    ->document('message1');
Unity
DocumentReference documentRef = db
	.Collection("Rooms").Document("RoomA")
	.Collection("Messages").Document("Message1");
C#‎

C#

כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

DocumentReference documentRef = db
    .Collection("Rooms").Document("RoomA")
    .Collection("Messages").Document("Message1");
Ruby
message_ref = firestore.col("rooms").doc("roomA").col("messages").doc("message1")

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

אוספי משנה מאפשרים לכם לארגן את הנתונים בצורה היררכית, כך שיהיה קל יותר לגשת אליהם. כדי לקבל את כל ההודעות ב-roomA, אפשר ליצור הפניה לאוסף המשנה messages ולבצע איתה אינטראקציה כמו עם כל הפניה אחרת לאוסף.

מסמכים באוספי משנה יכולים להכיל גם אוספי משנה, וכך אפשר להוסיף עוד רמות של נתונים בתצוגת עץ. אפשר להגדיר עד 100 רמות של נתונים בתצוגת עץ.

קבוצות של אוספים

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

לדוגמה, אם למסד הנתונים יש אוסף messages ברמת הבסיס, וגם roomA וגם roomB באוסף rooms מכילים אוספי משנה של messages, כל הפריטים הבאים שייכים לקבוצת האוספים messages:

  • messages
  • rooms/roomA/messages
  • rooms/roomB/messages

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