מודל נתונים של Cloud Firestore

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

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

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

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

לכתוב מסמכים

ב-Cloud 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

    • ‫ בהשתתפות

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

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

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

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

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

קובצי עזר

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

Web

import { doc } from "firebase/firestore";

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

Web

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

val alovelaceDocumentRef = db.collection("users").document("alovelace")

Java

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

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

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

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

C#‎

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

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

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

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

Web

import { collection } from "firebase/firestore";

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

Web

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

val usersCollectionRef = db.collection("users")

Java

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

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

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

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

C#‎

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

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

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

Web

import { doc } from "firebase/firestore"; 

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

Web

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

val alovelaceDocumentRef = db.document("users/alovelace")

Java

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

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

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

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

C#‎

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

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

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

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

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

  • חדרים

    • חדר אחד ()

      name : "my chat room"

    • חדרים

      ...

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

אוספי משנה

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

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

  • חדרים

    • חדר אחד ()

      name : "my chat room"

      • הודעות

        • הודעה1

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

        • message2

          ...

    • חדרים

      ...

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

Web

import { doc } from "firebase/firestore"; 

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

Web

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

val messageRef = db
    .collection("rooms").document("roomA")
    .collection("messages").document("message1")

Java

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

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

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

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

C#‎

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

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 רמות.