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

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

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

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

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

מסמכים

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

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

  • aloveace

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

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

  • aloveace

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

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

אוספים

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

  • משתמשים

    • aloveace

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

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

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

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

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

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

הפניות

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

Web modular API

import { doc } from "firebase/firestore";

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

Web namespaced API

var alovelaceDocumentRef = db.collection('users').doc('alovelace');
מָהִיר
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
let alovelaceDocumentRef = db.collection("users").document("alovelace")
Objective-C
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
FIRDocumentReference *alovelaceDocumentRef =
    [[self.db collectionWithPath:@"users"] documentWithPath:@"alovelace"];

Kotlin+KTX

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");
פִּיתוֹן
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');
אַחְדוּת
DocumentReference documentRef = db.Collection("users").Document("alovelace");
C#

C#

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

DocumentReference documentRef = db.Collection("users").Document("alovelace");
אוֹדֶם
document_ref = firestore.col("users").doc("alovelace")

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

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

Web modular API

import { collection } from "firebase/firestore";

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

Web namespaced API

var usersCollectionRef = db.collection('users');
מָהִיר
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
let usersCollectionRef = db.collection("users")
Objective-C
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
FIRCollectionReference *usersCollectionRef = [self.db collectionWithPath:@"users"];

Kotlin+KTX

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");
פִּיתוֹן
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');
אַחְדוּת
CollectionReference collectionRef = db.Collection("users");
C#

C#

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

CollectionReference collectionRef = db.Collection("users");
אוֹדֶם
collection_ref = firestore.col "users"

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

Web modular API

import { doc } from "firebase/firestore"; 

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

Web namespaced API

var alovelaceDocumentRef = db.doc('users/alovelace');
מָהִיר
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
let aLovelaceDocumentReference = db.document("users/alovelace")
Objective-C
הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
FIRDocumentReference *aLovelaceDocumentReference =
    [self.db documentWithPath:@"users/alovelace"];

Kotlin+KTX

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");
פִּיתוֹן
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');
אַחְדוּת
DocumentReference documentRef = db.Document("users/alovelace");
C#

C#

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

DocumentReference documentRef = db.Document("users/alovelace");
אוֹדֶם
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!"

        • הודעת 2

          ...

    • חדר ב

      ...

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

Web modular API

import { doc } from "firebase/firestore"; 

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

Web namespaced API

var messageRef = db.collection('rooms').doc('roomA')
                .collection('messages').doc('message1');
מָהִיר
הערה: מוצר זה אינו זמין ביעדי 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+KTX

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");
פִּיתוֹן
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');
אַחְדוּת
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");
אוֹדֶם
message_ref = firestore.col("rooms").doc("roomA").col("messages").doc("message1")

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

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

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