现已推出具有 MongoDB 兼容性的 Firestore 企业版!
了解详情。
選擇資料結構
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
請注意,在 Cloud Firestore 中建構資料時,您有幾種不同的選項:
請根據您的用途,考量每個選項的優點。本指南列出階層式資料的幾個範例結構。
文件中的巢狀資料
您可以在文件中巢狀嵌入陣列或對映等複雜物件。
- 優點:如果資料清單簡單且固定,並想保留在文件中,這種做法設定簡單,可簡化資料結構。
-
限制:
相較於其他選項,這個方法的可擴充性較低,
特別是當資料量隨時間增加時。清單越大或持續擴充,文件也會隨之變大,導致文件擷取時間變慢。
- 可能的用途為何?舉例來說,在即時通訊應用程式中,您可能會將使用者最近造訪的 3 個聊天室儲存為個人資料中的巢狀清單。
|
- class alovelace
- name :
first : "Ada"
last : "Lovelace"
born : 1815
rooms :
0 : "Software Chat"
1 : "Famous Figures"
2 : "Famous SWEs"
|
子集合
如果資料可能會隨時間擴充,您可以在文件中建立集合。
- 優點:隨著清單增加,父項文件的大小不會改變。您也可以對子集合執行完整查詢,並跨子集合發出
集合群組查詢。
-
限制:您無法輕鬆刪除子集合。
- 可能的用途為何?舉例來說,您可以在同一個聊天應用程式中,於聊天室文件內建立使用者或訊息的集合。
|
- collections_bookmark science
- class software
name : "software chat"
- collections_bookmark
users
- class
alovelace
first : "Ada"
last : "Lovelace"
- class
sride
first : "Sally"
last : "Ride"`
- class 天體物理學
|
根層級集合
在資料庫的根層級建立集合,整理不同的資料集。
- 優點:根層級集合適用於多對多關係,並可在每個集合中提供強大的查詢功能。
-
限制:隨著資料庫成長,擷取自然階層式資料可能會越來越複雜。
- 可能的用途為何?舉例來說,您可以在同一個即時通訊應用程式中,為使用者建立一個集合,並為聊天室和訊息建立另一個集合。
|
- collections_bookmark
使用者
- class alovelace
first : "Ada"
last : "Lovelace"
born : 1815
- class sride
first : "Sally"
last : "Ride"
born : 1951
- collections_bookmark 間會議室
- class 軟體
- collections_bookmark
訊息
- class
message1
from : "alovelace"
content : "..."
- class
message2
from : "sride"
content : "..."
|
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-16 (世界標準時間)。
[null,null,["上次更新時間:2025-08-16 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nRemember, when you structure your data in Cloud Firestore, you\nhave a few different options:\n\n- Documents\n- Multiple collections\n- Subcollections within documents\n\nConsider the advantages of each option as they\nrelate to your use case. A few example structures for hierarchical data\nare outlined in this guide.\n\n\u003cbr /\u003e\n\nNested data in documents\n\nYou can nest complex objects like arrays or maps within documents.\n\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|\n| - **Advantages:** If you have simple, fixed lists of data that you want to keep within your documents, this is easy to set up and streamlines your data structure. - **Limitations:** This isn't as scalable as other options, especially if your data expands over time. With larger or growing lists, the document also grows, which can lead to slower document retrieval times. - **What's a possible use case?** In a chat app, for example, you might store a user's 3 most recently visited chat rooms as a nested list in their profile. | - class alovelace - name : first : \"Ada\" last : \"Lovelace\" born : 1815 rooms : 0 : \"Software Chat\" 1 : \"Famous Figures\" 2 : \"Famous SWEs\" |\n\nSubcollections\n\nYou can create collections within documents when you have data that might expand\nover time.\n\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - **Advantages:** As your lists grow, the size of the parent document doesn't change. You also get full query capabilities on subcollections, and you can issue [collection group queries](../query-data/queries) across subcollections. - **Limitations:** You can't easily delete subcollections. - **What's a possible use case?** In the same chat app, for example, you might create collections of users or messages within chat room documents. | - collections_bookmark science - class software name : \"software chat\" - collections_bookmark users - class alovelace first : \"Ada\" last : \"Lovelace\" - class sride first : \"Sally\" last : \"Ride\"\\` \u003cbr /\u003e \u003cbr /\u003e - class astrophysics - ... |\n\nRoot-level collections\n\nCreate collections at the root level of your database to organize disparate data\nsets.\n\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - **Advantages:** Root-level collections are good for many-to-many relationships and provide powerful querying within each collection. - **Limitations:** Getting data that is naturally hierarchical might become increasingly complex as your database grows. - **What's a possible use case?** In the same chat app, for example, you might create one collection for users and another for rooms and messages. | - collections_bookmark users - class alovelace first : \"Ada\" last : \"Lovelace\" born : 1815 - class sride first : \"Sally\" last : \"Ride\" born : 1951 - collections_bookmark rooms - class software - collections_bookmark messages - class message1 from : \"alovelace\" content : \"...\" - class message2 from : \"sride\" content : \"...\" |"]]