firestore data structure

Choose a Data Structure

Remember, when you structure your data in Cloud Firestore, you have a few different options: documents, multiple collections, and subcollections within documents. Consider the advantages of each option as they relate to your use case.

Nested data in documents

You can nest complex objects like arrays (maps) within documents.

  • 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: You can't run queries on nested lists. Additionally, 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"

Subcollections

You can create collections within documents when you have data that might expand over time.

  • Advantages: As your lists grow, the size of the parent document doesn't change. You also get full query capabilities on subcollections.
  • Limitations: You can't easily delete subcollections, or perform compound queries across 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"`


    • class astrophysics
      • ...

Root-level collections

Create collections at the root level of your database to organize disparate data sets.

  • Advantages: Root-level collections offer the most flexibility and scalability, along with 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 : "..."
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值