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

Cloud Firestore là cơ sở dữ liệu định hướng tài liệu, NoSQL. 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 các tài liệu đượ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 khóa-giá trị. Cloud Firestore được tối ưu hóa để lưu trữ bộ sưu tập lớn các tài liệu nhỏ.

Tất cả các tài liệu phải được lưu trữ trong bộ sưu tập. Tài liệu có thể chứa các bộ sưu tậ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 thủy như chuỗi hoặc các đối tượng phức tạp như danh sách.

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 một tài liệu trong bộ sưu tập. Nếu bộ sưu tập hoặc tài liệu không tồn tại, Cloud Firestore sẽ tạo nó.

Các 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 nhẹ chứa các trường ánh xạ tới 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 alovelace của người dùng có thể trông như thế này:

  • alovelace

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

Các đối tượng phức tạp, lồng nhau trong 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 từ 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 rất giống JSON. Trên thực tế, về cơ bản là như vậy. 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 kích thước ở 1 MB), nhưng nói 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 tồn tại trong các bộ sưu tập, đơn giản là nơi 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 của mình, 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

    • học

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

Cloud Firestore không có sơ đồ nên bạn hoàn toàn có quyền tự do quyết định những trường nào bạn đặt trong mỗi tài liệu và loại dữ liệu nào 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 đề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à loại dữ liệu trên nhiều tài liệu để có thể truy vấn tài liệu dễ dàng hơn.

Một bộ sưu tập chứa các tài liệu và không có gì khác. Nó không thể chứa trực tiếp các trường thô có giá trị và không thể chứa các bộ sưu tập khác. (Xem Dữ liệu phân cấp để biết giải thích về 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 khóa của riêng mình, chẳng hạn như ID người dùng hoặc bạn có thể để Cloud Firestore tự động tạo ID ngẫu nhiên cho bạn.

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

Người giới thiệu

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

Web modular API

import { doc } from "firebase/firestore";

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

Web namespaced API

var alovelaceDocumentRef = db.collection('users').doc('alovelace');
Nhanh
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip.
let alovelaceDocumentRef = db.collection("users").document("alovelace")
Mục tiêu-C
Lưu ý: Sản phẩm này không khả dụ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');
Đi

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

func createDocReference(client *firestore.Client) {

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

	_ = alovelaceRef
}
PHP

PHP

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

$document = $db->collection('samples/php/users')->document('alovelace');
Đoàn kết
DocumentReference documentRef = db.Collection("users").Document("alovelace");
C#

C#

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

DocumentReference documentRef = db.Collection("users").Document("alovelace");
hồng ngọc
document_ref = firestore.col("users").doc("alovelace")

Tham chiếu là một đối tượng nhẹ chỉ trỏ đến một vị trí trong cơ sở dữ liệu của bạn. 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 không thực hiện bất kỳ thao tác mạng nào.

Bạn cũng có thể tạo tài liệu tham khảo cho các bộ sưu tập :

Web modular API

import { collection } from "firebase/firestore";

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

Web namespaced API

var usersCollectionRef = db.collection('users');
Nhanh
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip.
let usersCollectionRef = db.collection("users")
Mục tiêu-C
Lưu ý: Sản phẩm này không khả dụ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');
Đi

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

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

	_ = usersRef
}
PHP

PHP

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

$collection = $db->collection('samples/php/users');
Đoàn kết
CollectionReference collectionRef = db.Collection("users");
C#

C#

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

CollectionReference collectionRef = db.Collection("users");
hồng ngọc
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 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 lên ( / ). Ví dụ: để tạo một tham chiếu đến tài liệu alovelace :

Web modular API

import { doc } from "firebase/firestore"; 

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

Web namespaced API

var alovelaceDocumentRef = db.doc('users/alovelace');
Nhanh
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip.
let aLovelaceDocumentReference = db.document("users/alovelace")
Mục tiêu-C
Lưu ý: Sản phẩm này không khả dụ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');
Đi

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 về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .

$document = $db->document('users/alovelace');
Đoàn kết
DocumentReference documentRef = db.Document("users/alovelace");
C#

C#

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

DocumentReference documentRef = db.Document("users/alovelace");
hồng ngọc
document_path_ref = firestore.doc "users/alovelace"

Dữ liệu phân cấp

Để hiểu cách cấu trúc dữ liệu phân cấp hoạt động trong Cloud Firestore, hãy xem xét một ứng dụng trò chuyện mẫu 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ữ các phòng trò chuyện khác nhau:

  • bộ phòng

    • phòng

      name : "my chat room"

    • phòng

      ...

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

Bộ sưu tập con

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

  • bộ phòng

    • phòng

      name : "my chat room"

      • tin nhắn

        • tin nhắn 1

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

        • tin nhắn 2

          ...

    • phòng

      ...

Trong ví dụ này, bạn sẽ tạo một tham chiếu đến một tin nhắn trong bộ sưu tập con với mã sau:

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');
Nhanh
Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu watchOS và App Clip.
let messageRef = db
  .collection("rooms").document("roomA")
  .collection("messages").document("message1")
Mục tiêu-C
Lưu ý: Sản phẩm này không khả dụ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');
Đi

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 về cách cài đặt và tạo ứng dụng khách Cloud Firestore, hãy tham khảo Thư viện ứng dụng khách Cloud Firestore .

$document = $db
    ->collection('rooms')
    ->document('roomA')
    ->collection('messages')
    ->document('message1');
Đoàn kết
DocumentReference documentRef = db
	.Collection("Rooms").Document("RoomA")
	.Collection("Messages").Document("Message1");
C#

C#

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

DocumentReference documentRef = db
    .Collection("Rooms").Document("RoomA")
    .Collection("Messages").Document("Message1");
hồng ngọc
message_ref = firestore.col("rooms").doc("roomA").col("messages").doc("message1")

Lưu ý mô hình xen kẽ của các bộ sưu tập và tài liệu. 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 bộ sưu tập trong bộ sưu tập hoặc tài liệu trong tài liệu.

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

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 thêm dữ liệu. Bạn có thể lồng dữ liệu sâu tới 100 cấp độ.