Firebase is back at Google I/O on May 10! Register now

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

Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.

Cloud Firestore là một 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 các bộ sưu tập. Tài liệu có thể chứa các tập hợp con và đố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 đố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 hoàn toàn trong Cloud Firestore. Chỉ cần gán dữ liệu cho một tài liệu trong một 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ởi một tên.

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

  • học

    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 từ ví dụ trên bằng bản đồ, như sau:

  • học

    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 chúng 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à có kích thước giới hạn ở 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 sống trong các bộ sưu tập, chỉ đơ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 được đại diện bởi một tài liệu:

  • người dùng

    • học

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

    • học

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

Cloud Firestore không có sơ đồ, vì vậy bạn có toàn quyền 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 bạn lưu trữ trong những trường đó. Tất cả các tài liệu trong cùng một bộ sưu tập 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 để bạn 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" các 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ả các tài liệu trong một 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 bởi 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 version 9

import { doc } from "firebase/firestore";

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

Web version 8

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");
con trăn
a_lovelace_ref = db.collection(u'users').document(u'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
alovelaceRef := client.Collection("users").Doc("alovelace")
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 tham chiếu đến các bộ sưu tập :

Web version 9

import { collection } from "firebase/firestore";

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

Web version 8

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");
con trăn
users_ref = db.collection(u'users')

Python

users_ref = db.collection("users")
C++
CollectionReference users_collection_reference = db->Collection("users");
Node.js
const usersCollectionRef = db.collection('users');
Đi
usersRef := client.Collection("users")
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 version 9

import { doc } from "firebase/firestore"; 

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

Web version 8

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");
con trăn
a_lovelace_ref = db.document(u'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
// Reference to a document with id "alovelace" in the collection "users"
alovelaceRef := client.Doc("users/alovelace")
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 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 ứ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 A

      name : "my chat room"

    • phòng

      ...

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 số lượng lớn thư. 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 các 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 bộ sưu tập con. Một bộ sưu tập con là một 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 phòng trong bộ sưu tập rooms của bạn:

  • bộ phòng

    • phòng A

      name : "my chat room"

      • tin nhắn

        • tin nhắn

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

        • tin nhắn

          ...

    • phòng

      ...

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

Web version 9

import { doc } from "firebase/firestore"; 

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

Web version 8

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");
con trăn
room_a_ref = db.collection(u'rooms').document(u'roomA')
message_ref = room_a_ref.collection(u'messages').document(u'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
messageRef := client.Collection("rooms").Doc("roomA").
	Collection("messages").Doc("message1")
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. 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 thứ bậc, giúp truy cập dữ liệu dễ dàng hơn. Để nhận tất cả thư trong roomA , bạn có thể tạo một tham chiếu bộ sưu tập cho messages của 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 độ.