MongoDB 互換の Firestore Enterprise エディションが利用可能になりました。
詳細
データ構造の選択
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
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
|
ルートレベルのコレクション
性質の異なるデータセットをまとめるには、データベースのルートレベルでコレクションを作成します。
- 利点: ルートレベルのコレクションは、多対多の関係に適しており、各コレクション内で強力なクエリを実行できます。
- 制限: データが階層的になっていることから、データベースが拡大するにつれ、データの取得が難しくなる可能性があります。
- 適用可能なユースケース: 同じチャットアプリで、ユーザー用に 1 つ、チャットルームとメッセージ用に 1 つそれぞれコレクションを作成する。
|
- 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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-16 UTC。
[null,null,["最終更新日 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 : \"...\" |"]]