Firebase数据库数据聚合

Firebase数据库数据聚合,firebase,join,firebase-realtime-database,nosql,Firebase,Join,Firebase Realtime Database,Nosql,让我们以“类似Instagram”的应用程序为例 在feed中,我们得到了帖子,用户头像和名字在顶部,照片或视频在下面,最后的评论,喜欢计数和发布时间在底部 基本上,在客户端,我正在等待从后端获取类似 { 用户名:“John”, 阿凡达:“一些链接”, 照片:“照片url”, 喜欢:“9”, 时间:“182937428”, 评论:[那里的评论] } 但是使用Firebase,我需要以更平坦的方式存储数据。因此,在数据JSON中会有“用户”、“帖子”和“评论” 我该如何将来自这些节点的数据聚合到某

让我们以“类似Instagram”的应用程序为例

在feed中,我们得到了帖子,用户头像和名字在顶部,照片或视频在下面,最后的评论,喜欢计数和发布时间在底部

基本上,在客户端,我正在等待从后端获取类似

{
用户名:“John”,
阿凡达:“一些链接”,
照片:“照片url”,
喜欢:“9”,
时间:“182937428”,
评论:[那里的评论]
}

但是使用Firebase,我需要以更平坦的方式存储数据。因此,在数据JSON中会有“用户”、“帖子”和“评论”

我该如何将来自这些节点的数据聚合到某种单一对象中,这在客户端很容易使用

或者我应该要求Firebase提供帖子,而不是所有用户的帖子,以及他们的所有评论,并在完成所有三个“请求”后进行聚合吗

您应该实现“浅”树结构,并在需要时使用引用。 这意味着在应用程序中的大多数情况下,您应该按原样使用对象,确保它包含“基本数据”(在下面的示例中为“聊天标题”)和“进一步”信息的键(在示例中为“成员”的键)

从firebase文档():

坏的

{
  // This is a poorly nested data architecture, because iterating the children
  // of the "chats" node to get a list of conversation titles requires
  // potentially downloading hundreds of megabytes of messages
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "messages": {
        "m1": { "sender": "ghopper", "message": "Relay malfunction found. Cause: moth." },
        "m2": { ... },
        // a very long list of messages
      }
    },
    "two": { ... }
  }
}
{
  // Chats contains only meta info about each conversation
  // stored under the chats's unique ID
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
      "timestamp": 1459361875666
    },
    "two": { ... },
    "three": { ... }
  },

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

  // Messages are separate from data we may want to iterate quickly
  // but still easily paginated and queried, and organized by chat
  // conversation ID
  "messages": {
    "one": {
      "m1": {
        "name": "eclarke",
        "message": "The relay seems to be malfunctioning.",
        "timestamp": 1459361875337
      },
      "m2": { ... },
      "m3": { ... }
    },
    "two": { ... },
    "three": { ... }
  }
}

{
  // This is a poorly nested data architecture, because iterating the children
  // of the "chats" node to get a list of conversation titles requires
  // potentially downloading hundreds of megabytes of messages
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "messages": {
        "m1": { "sender": "ghopper", "message": "Relay malfunction found. Cause: moth." },
        "m2": { ... },
        // a very long list of messages
      }
    },
    "two": { ... }
  }
}
{
  // Chats contains only meta info about each conversation
  // stored under the chats's unique ID
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
      "timestamp": 1459361875666
    },
    "two": { ... },
    "three": { ... }
  },

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

  // Messages are separate from data we may want to iterate quickly
  // but still easily paginated and queried, and organized by chat
  // conversation ID
  "messages": {
    "one": {
      "m1": {
        "name": "eclarke",
        "message": "The relay seems to be malfunctioning.",
        "timestamp": 1459361875337
      },
      "m2": { ... },
      "m3": { ... }
    },
    "two": { ... },
    "three": { ... }
  }
}

我还是不明白。那么,如果我用“阿凡达”、“姓名”、“年龄”来表示“用户”呢。我得到了带有“url”、“count”和“userId”的“post”。如何把它组合起来?手动为每一个“帖子”创建一个“帖子”,并将“user.avatar”和“user.name”添加到客户端实现上的its中?如果有成千上万的帖子呢??或者我应该将数据从用户复制到帖子?若用户更改了名称或头像呢???