如何在Firebase中构建数据?

时间:2022-09-06 17:10:53

i come from a MySQL workflow and now i am trying to do something in Firebase, and i have some dilemmas with structuring my data, because i don't know how would i query it.

我来自MySQL工作流程,现在我正在尝试在Firebase中做一些事情,并且我有一些构造我的数据的困境,因为我不知道我将如何查询它。

fallowing the example below where i have some users and some comments, what is a good way to find

在下面的示例中,我有一些用户和一些评论,这是一个很好的方法

how many post likes a user had
how many post comments a user had
what were the posts that a user liked
...

i was thinking on adding that information to a user, like:

我正在考虑将这些信息添加到用户,例如:

  "user:2": {
    "name": "Doe",
    "email": "doe@bob.com",
    "post_likes": {
      "post:1": 1,
      "post:2": 1
    },
    "post_comments": {
      "post:1": 15,
      "post:2": 5
    }
  }

but this seems redundant and duplicates data..

但这似乎是多余的,并重复数据..

i need to find a way to search in posts everything that has to do with user:1, maybe i need to create another object that connects the users and posts ??

我需要找到一种方法在帖子中搜索与用户有关的所有内容:1,也许我需要创建另一个连接用户和帖子的对象?

any ideas?

有任何想法吗?

{
  "array": {
    "users": {
      "user:1": {
        "name": "John",
        "email": "john@bob.com"
      },
      "user:2": {
        "name": "Doe",
        "email": "doe@bob.com"
      }
    },
    "posts": {
      "post:1": {
        "name": "First Post",
        "content": "some post content",
        "comments": {}
      },
      "post:2": {
        "name": "Second Post",
        "content": "some other post content",
        "likes": 2,
        "comments": {
          "comment:1": {
            "uid": "user:1",
            "email": "some comment"
          },
          "comment:2": {
            "uid": "user:2",
            "email": "some other comment"
          }
        }
      }
    }
  }
}

1 个解决方案

#1


2  

How to structure data is not a simple question to answer and is highly dependent on how the data will be read. The general rule is to work hard on writes to make reads easy. Firebase offers two guides on understanding data and on structuring data.

如何构建数据不是一个简单的问题,而是高度依赖于如何读取数据。一般规则是努力写入以使读取变得容易。 Firebase提供了两个关于理解数据和构建数据的指南。

They won't fit nicely into this text box, so you'll find better answers there. Here's a birds-eye view.

它们不适合这个文本框,所以你会在那里找到更好的答案。这是一个鸟瞰图。

  1. It's JSON data.
  2. 这是JSON数据。
  3. Flatten your data. Don't nest just because you can. Treat each logical data component like you would in SQL and keep it in its own path.
  4. 展平您的数据。不要因为你可以筑巢。像处理SQL一样处理每个逻辑数据组件并将其保存在自己的路径中。
  5. Avoid arrays in distributed systems. Sequential numeric ids are error prone.
  6. 避免分布式系统中的数组。顺序数字ID容易出错。
  7. Utilize indices to create sub-lists of master data rather than trying to nest lists in lists in lists.
  8. 利用索引创建主数据的子列表,而不是尝试在列表中的列表中嵌套列表。

#1


2  

How to structure data is not a simple question to answer and is highly dependent on how the data will be read. The general rule is to work hard on writes to make reads easy. Firebase offers two guides on understanding data and on structuring data.

如何构建数据不是一个简单的问题,而是高度依赖于如何读取数据。一般规则是努力写入以使读取变得容易。 Firebase提供了两个关于理解数据和构建数据的指南。

They won't fit nicely into this text box, so you'll find better answers there. Here's a birds-eye view.

它们不适合这个文本框,所以你会在那里找到更好的答案。这是一个鸟瞰图。

  1. It's JSON data.
  2. 这是JSON数据。
  3. Flatten your data. Don't nest just because you can. Treat each logical data component like you would in SQL and keep it in its own path.
  4. 展平您的数据。不要因为你可以筑巢。像处理SQL一样处理每个逻辑数据组件并将其保存在自己的路径中。
  5. Avoid arrays in distributed systems. Sequential numeric ids are error prone.
  6. 避免分布式系统中的数组。顺序数字ID容易出错。
  7. Utilize indices to create sub-lists of master data rather than trying to nest lists in lists in lists.
  8. 利用索引创建主数据的子列表,而不是尝试在列表中的列表中嵌套列表。