现已推出具有 MongoDB 兼容性的 Firestore 企业版!
了解详情。
Wybierz strukturę danych
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Pamiętaj, że podczas strukturyzowania danych w Cloud Firestore masz kilka różnych opcji:
- Dokumenty
- Wiele kolekcji
- Podzbiory w dokumentach
Rozważ zalety każdej opcji w kontekście Twojego przypadku użycia. W tym przewodniku znajdziesz kilka przykładowych struktur danych hierarchicznych.
Zagnieżdżone dane w dokumentach
W dokumentach możesz zagnieżdżać złożone obiekty, takie jak tablice lub mapy.
- Zalety: jeśli masz proste, stałe listy danych, które chcesz zachować w dokumentach, ta metoda jest łatwa do skonfigurowania i upraszcza strukturę danych.
-
Ograniczenia:
to rozwiązanie nie jest tak skalowalne jak inne opcje, zwłaszcza jeśli Twoje dane z czasem się rozrosną. Wraz z powiększaniem się listy rośnie też dokument, co może wydłużyć czas jego pobierania.
- Jaki jest możliwy przypadek użycia? Na przykład w aplikacji do czatowania możesz przechowywać 3 ostatnio odwiedzone przez użytkownika pokoje czatu jako zagnieżdżoną listę w jego profilu.
|
- class alovelace
- name :
first : "Ada"
last : "Lovelace"
born : 1815
rooms :
0 : "Software Chat"
1 : "Famous Figures"
2 : "Famous SWEs"
|
Podkolekcje
Kolekcje w dokumentach możesz tworzyć, gdy masz dane, które mogą się z czasem rozszerzać.
- Zalety: w miarę powiększania się list rozmiar dokumentu nadrzędnego nie zmienia się. Masz też pełne możliwości wykonywania zapytań dotyczących podzbiorów i możesz wysyłać
zapytania do grup kolekcji w podzbiorach.
-
Ograniczenia: nie można łatwo usuwać podkolekcji.
- Jaki jest możliwy przypadek użycia? W tej samej aplikacji do czatowania możesz na przykład tworzyć kolekcje użytkowników lub wiadomości w dokumentach pokoju czatu.
|
- collections_bookmark science
- class oprogramowanie
name : "software chat"
- collections_bookmark
użytkowników
- class
alovelace
first : "Ada"
last : "Lovelace"
- class
sride
first : "Sally"
last : "Ride"`
- class astrofizyka,
|
Kolekcje najwyższego poziomu
Twórz kolekcje na poziomie głównym bazy danych, aby porządkować różne zbiory danych.
- Zalety: kolekcje najwyższego poziomu są odpowiednie w przypadku relacji wiele-do-wielu i umożliwiają zaawansowane wyszukiwanie w każdej kolekcji.
-
Ograniczenia: pobieranie danych, które są naturalnie hierarchiczne, może stawać się coraz bardziej złożone wraz z rozwojem bazy danych.
- Jaki jest możliwy przypadek użycia? W tej samej aplikacji do czatowania możesz na przykład utworzyć jedną kolekcję dla użytkowników, a drugą dla pokoi i wiadomości.
|
- collections_bookmark
użytkowników
- class alovelace
first : "Ada"
last : "Lovelace"
born : 1815
- class sride
first : "Sally"
last : "Ride"
born : 1951
- collections_bookmark sal
- class oprogramowanie,
- collections_bookmark
wiadomości
- class
message1
from : "alovelace"
content : "..."
- class
message2
from : "sride"
content : "..."
|
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-08-16 UTC.
[null,null,["Ostatnia aktualizacja: 2025-08-16 UTC."],[],[],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 : \"...\" |"]]