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

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

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

Tất cả tài liệu phải được lưu trữ trong các bộ sưu tập. Tài liệu có thể chứa tập hợp con và các đối tượng lồng nhau. Cả hai đều có thể bao gồm các trường nguyên thuỷ như chuỗi hoặc cá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 một cách gián tiếp trong Cloud Firestore. Chỉ cần chỉ định dữ liệu cho một tài liệu trong một bộ sưu tập. Nếu không có bộ sưu tập hoặc tài liệu nào, Cloud Firestore sẽ tạo bộ sưu tập hoặc tài liệu đó.

Tài liệu

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

Một tài liệu đại diện cho người dùng alovelace có thể trông như sau:

  • alovelace

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

Các đối tượng phức tạp, lồng nhau 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 người dùng trong ví dụ ở trên bằng một bản đồ, như sau:

  • alovelace

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

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

Bộ sưu tập

Tài liệu nằm trong các bộ sưu tập, đây chỉ là các 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, mỗi người dùng được biểu thị bằng một tài liệu:

  • người dùng

    • alovelace

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

    • aturing

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

Cloud Firestore không có giản đồ, vì vậy bạn hoàn toàn có thể tự do quyết định những trường bạn đặt trong mỗi tài liệu và những 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 tập hợp đều có thể chứa các trường khác nhau hoặc lưu trữ các loại dữ liệu khác nhau trong các trường đó. Tuy nhiên, bạn nên sử dụng cùng các trường và kiểu dữ liệu trên nhiều tài liệu để có thể dễ dàng truy vấn các tài liệu hơn.

Một bộ sưu tập chỉ chứa các tài liệu. Không thể trực tiếp chứa các trường thô có giá trị và không thể chứa các tập hợp khác. (Xem phần Dữ liệu phân cấp để biết cách cấu trúc dữ liệu 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à duy nhất. Bạn có thể cung cấp khoá riêng (chẳng hạn như mã nhận dạng người dùng) hoặc cho phép Cloud Firestore tự động tạo mã nhận dạng ngẫu nhiên cho bạn.

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

Tài liệu tham khảo

Mọi tài liệu trong Cloud Firestore đều được xác định duy nhất 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ã, bạn có thể tạo một tài liệu tham khảo cho vị trí đó.

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 có 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 có trên các mục tiêu watchOS và 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');
Tìm

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")

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

Bạn cũng có thể tạo các tham chiếu đến 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 có 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 có trên các mục tiêu watchOS và 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');
Tìm

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 các tham chiếu bằng cách chỉ định đường dẫn đến một tài liệu hoặc tập hợ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 dấu gạch chéo (/). Ví dụ: để tạo một 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 có 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 có trên các mục tiêu watchOS và 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');
Tìm

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 xem xét 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

    • roomA

      name : "my chat room"

    • roomB

      ...

Giờ đây, bạn đã có phòng trò chuyện, hãy quyết định cách lưu trữ tin nhắn. Bạn có thể không muốn lưu trữ các tệp này trong tài liệu của phòng trò chuyện. Các tài liệu trong Cloud Firestore phải có dung lượng nhỏ và một phòng trò chuyện có thể chứa một số lượng lớn tin nhắn. Tuy nhiên, bạn có thể tạo các tập hợp bổ sung 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 con

Cách tốt nhất để lưu trữ thông báo trong trường hợp này là sử dụng các bộ sưu tập con. Tập hợ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 phòng trong bộ sưu tập rooms:

  • phòng

    • roomA

      name : "my chat room"

      • tin nhắn

        • message1

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

        • message2

          ...

    • roomB

      ...

Trong ví dụ này, bạn sẽ tạo một tham chiếu đến một thông báo trong tập hợ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 có 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 có 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

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');
Tìm

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")

Lưu ý mẫu xen kẽ của các bộ sưu tập và tài liệu. Các bộ sưu tập và tài liệu của bạn 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.

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

Các tài liệu trong 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 lồng dữ liệu sâu hơn. Bạn có thể lồng dữ liệu tối đa 100 cấp.