现已推出具有 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 astrophysics
|
根级集合
在数据库的根级别上创建集合,以整理不同的数据集。
- 优点:根级集合适合多对多关系,并在每个集合内提供强大的查询功能。
- 限制:随着数据库增大,获取自然分层的数据可能会变得越来越复杂。
- 此方案适用于哪些使用场景?例如,在前述聊天应用中,您可以为用户创建一个集合,为聊天室和消息创建另一个集合。
|
- 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 : "..."
|
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-16。
[null,null,["最后更新时间 (UTC):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 : \"...\" |"]]