מודל נתונים של 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. למעשה, הן כמעט זהות. יש כמה הבדלים (לדוגמה, מסמכים תומכים בסוגי נתונים נוספים והגודל שלהם מוגבל ל-1MB), אבל באופן כללי, אפשר להתייחס למסמכים כרשומות JSON קלות משקל.

אוספים

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

  • משתמשים

    • alovelace

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

    • aturing

      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 וביעדים של קליפים של אפליקציות.
let alovelaceDocumentRef = db.collection("users").document("alovelace")
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS וביעדים של קליפים של אפליקציות.
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');
Go

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 וביעדים של קליפים של אפליקציות.
let usersCollectionRef = db.collection("users")
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS וביעדים של קליפים של אפליקציות.
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');
Go

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 וביעדים של קליפים של אפליקציות.
let aLovelaceDocumentReference = db.document("users/alovelace")
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS וביעדים של קליפים של אפליקציות.
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');
Go

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 כדי לאחסן בו חדרי צ'אט שונים:

  • חדרים

    • roomA

      name : "my chat room"

    • roomB

      ...

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

אוספי משנה

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

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

  • חדרים

    • roomA

      name : "my chat room"

      • messages

        • message1

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

        • message2

          ...

    • roomB

      ...

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

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 וביעדים של קליפים של אפליקציות.
let messageRef = db
  .collection("rooms").document("roomA")
  .collection("messages").document("message1")
Objective-C
הערה: המוצר הזה לא זמין ב-watchOS וביעדים של קליפים של אפליקציות.
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');
Go

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