Mô hình dữ liệu trên Cloud Firestore

Cloud Firestore là một cơ sở dữ liệu NoSQL, hướng tài liệu. Không giống như cơ sở dữ liệu SQL, không có bảng hoặc hàng. Thay vào đó, bạn lưu trữ dữ liệu trong tài liệu, tức là được sắp xếp thành các bộ sưu tập.

Mỗi tài liệu chứa một tập hợp các cặp khoá-giá trị. Cloud Firestore là được tối ưu hoá để lưu trữ các bộ sưu tập lớn gồm các tài liệu nhỏ.

Mọi tài liệu phải được lưu trữ trong bộ sưu tập. Tài liệu có thể chứa bộ sưu tập con và đối tượng lồng nhau, cả hai đều có thể chứa các trường gốc như chuỗi hoặc đối tượng phức tạp như danh sách.

Các bộ sưu tập và tài liệu được tạo ngầm trong Cloud Firestore. Chỉ cần gán dữ liệu cho tài liệu trong một bộ sưu tập. Nếu một trong hai bộ sưu tập hoặc tài liệu không tồn tại, Cloud Firestore sẽ tạo tài liệu đó.

Tài liệu

Trong Cloud Firestore, đơn vị lưu trữ chính là tài liệu. Tài liệu là một bản ghi nhẹ chứa các trường, liên kết với các giá trị. Mỗi tài liệu được xác định theo tên.

Tài liệu đại diện cho người dùng alovelace có thể có dạng như sau:

  • Vòng yêu

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

Các đối tượng phức tạp, được lồng trong một tài liệu được gọi là bản đồ. Ví dụ: bạn có thể cấu trúc tên của người dùng từ ví dụ ở trên bằng một bản đồ, như sau:

  • Vòng yêu

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

Bạn có thể thấy rằng tài liệu trông rất giống JSON. Trên thực tế, về cơ bản là chúng. Có một số điểm khác biệt (ví dụ: tài liệu hỗ trợ các loại dữ liệu bổ sung và bị giới hạn về kích thước ở mức 1 MB), nhưng nhìn chung, bạn có thể coi tài liệu là bản ghi JSON nhẹ.

Bộ sưu tập

Tài liệu nằm trong bộ sưu tập, đơn giản là vùng chứa tài liệu. Để Ví dụ: bạn có thể có một bộ sưu tập users để chứa nhiều người dùng khác nhau, mỗi người dùng được trình bày bằng một tài liệu:

  • người dùng

    • Vòng yêu

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

    • Đang tham gia

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

Cloud Firestore không có giản đồ nên bạn có thể hoàn toàn tự do các trường bạn đưa vào mỗi tài liệu và loại dữ liệu bạn lưu trữ trong các trường đó. Các tài liệu trong cùng một bộ sưu tập có thể chứa các trường hoặc kho lưu trữ khác nhau dữ liệu khác nhau trong các trường đó. Tuy nhiên, bạn nên sử dụng các trường và loại dữ liệu giống nhau trên nhiều tài liệu để bạn có thể truy vấn tài liệu dễ dàng hơn.

Bộ sưu tập chứa các tài liệu và không có nội dung nào khác. Dữ liệu này không được trực tiếp chứa dữ liệu thô các trường có giá trị và không thể chứa các bộ sưu tập khác. (Xem phần Phân cấp Dữ liệu để giải thích cách xây dựng cấu trúc phức tạp hơn trong Cloud Firestore.)

Tên của các tài liệu trong một bộ sưu tập là không trùng lặp. Bạn có thể tự cung cấp các khoá, chẳng hạn như mã nhận dạng người dùng hoặc bạn có thể cho phép Cloud Firestore tạo mã nhận dạng ngẫu nhiên cho bạn.

Bạn không cần phải "tạo" hoặc "delete" bộ sưu tập. Sau khi bạn tạo nhóm quảng cáo đầu tiên tài liệu trong bộ sưu tập thì bộ sưu tập đó tồn tại. Nếu bạn xoá tất cả tài liệu trong bộ sưu tập, tập hợp này không còn tồn tại.

Tài liệu tham khảo

Mỗi tài liệu trong Cloud Firestore đều được xác định riêng theo vị trí của tài liệu trong cơ sở dữ liệu. Ví dụ trước cho thấy một tài liệu alovelace trong bộ sưu tập users. Để tham chiếu đến vị trí này trong mã của mình, bạn có thể tạo một tham chiếu đến công cụ đó.

Web

import { doc } from "firebase/firestore";

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

Web

var alovelaceDocumentRef = db.collection('users').doc('alovelace');
Swift
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
let alovelaceDocumentRef = db.collection("users").document("alovelace")
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và 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");
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');
Tiến hành

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

func createDocReference(client *firestore.Client) {

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

	_ = alovelaceRef
}
PHP

PHP

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

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

C#

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

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

Tệp đối chiếu là một đối tượng nhẹ chỉ trỏ đến một vị trí trong cơ sở dữ liệu. Bạn có thể tạo tham chiếu cho dù dữ liệu có tồn tại ở đó hay không, và việc tạo tham chiếu sẽ không thực hiện bất kỳ hoạt động mạng nào.

Bạn cũng có thể tạo tệp tham chiếu cho bộ sưu tập:

Web

import { collection } from "firebase/firestore";

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

Web

var usersCollectionRef = db.collection('users');
Swift
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
let usersCollectionRef = db.collection("users")
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và 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");
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');
Tiến hành

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

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

	_ = usersRef
}
PHP

PHP

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

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

C#

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

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

Để thuận tiện, bạn cũng có thể tạo tham chiếu bằng cách chỉ định đường dẫn đến tài liệu hoặc bộ sưu tập dưới dạng một chuỗi, với các thành phần đường dẫn được phân tách bằng một dấu x dấu gạch chéo (/). Ví dụ: để tạo tham chiếu đến tài liệu alovelace:

Web

import { doc } from "firebase/firestore"; 

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

Web

var alovelaceDocumentRef = db.doc('users/alovelace');
Swift
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
let aLovelaceDocumentReference = db.document("users/alovelace")
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và 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");
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');
Tiến hành

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

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

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

C#

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng Cloud Firestore.

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

Dữ liệu phân cấp

Để hiểu cách hoạt động của cấu trúc dữ liệu phân cấp trong Cloud Firestore, hãy cân nhắc một ví dụ về ứng dụng trò chuyện có tin nhắn và phòng trò chuyện.

Bạn có thể tạo một bộ sưu tập có tên là rooms để lưu trữ nhiều phòng trò chuyện:

  • phòng

    • phòngA

      name : "my chat room"

    • phòngB

      ...

Vì bạn đã có phòng trò chuyện, hãy quyết định cách lưu trữ tin nhắn. Có thể bạn không phải muốn lưu trữ chúng trong tài liệu của phòng trò chuyện. Tài liệu trong Cloud Firestore cần phải gọn nhẹ và một phòng trò chuyện có thể chứa số lượng lớn tin nhắn. Tuy nhiên, bạn có thể tạo thêm các bộ sưu tập khác trong tài liệu của phòng trò chuyện, dưới dạng tập hợp con.

Bộ sưu tập phụ

Cách tốt nhất để lưu trữ thông báo trong trường hợp này là sử dụng bộ sưu tập con. Đáp bộ sưu tập con là một tập hợp được liên kết với một tài liệu cụ thể.

Bạn có thể tạo một bộ sưu tập con có tên là messages cho mọi tài liệu trong phòng bộ sưu tập rooms của bạn:

  • phòng

    • phòngA

      name : "my chat room"

      • tin nhắn

        • tin nhắn1

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

        • thông báo 2

          ...

    • phòngB

      ...

Trong ví dụ này, bạn sẽ tạo tham chiếu đến thông báo trong bộ sưu tập con bằng mã sau:

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
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và App Clip.
let messageRef = db
  .collection("rooms").document("roomA")
  .collection("messages").document("message1")
Objective-C
Lưu ý: Sản phẩm này không hoạt động trên các mục tiêu watchOS và 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");
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');
Tiến hành

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

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

	_ = messageRef
}
PHP

PHP

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng 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#

Để biết thêm thông tin về cách cài đặt và tạo ứng dụng Cloud Firestore, hãy tham khảo Thư viện ứng dụng 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")

Hãy lưu ý mẫu xen kẽ của tập hợp và tài liệu. Bộ sưu tập của bạn và tài liệu phải luôn tuân theo mẫu này. Bạn không thể tham chiếu một bộ sưu tập trong một bộ sưu tập hoặc một tài liệu trong một tài liệu.

Tập hợp con cho phép bạn sắp xếp dữ liệu theo hệ phân cấp, giúp dữ liệu dễ dàng được truy cập. Để xem tất cả thư trong roomA, bạn có thể tạo một tệp tham chiếu bộ sưu tập vào bộ sưu tập con messages và tương tác với bộ sưu tập đó như cách bạn làm tham chiếu bộ sưu tập.

Tài liệu trong các bộ sưu tập con cũng có thể chứa các bộ sưu tập con, cho phép bạn thêm dữ liệu lồng nhau. Bạn có thể lồng dữ liệu với tối đa 100 cấp độ.