【问题标题】:Storing data on Firebase to be queried later在 Firebase 上存储数据以供稍后查询
【发布时间】:2018-06-27 17:25:03
【问题描述】:

我们有一个应用程序可以将数据存储在 Firebase(数据库)上,稍后将对其进行查询。

存储数据的正确格式是什么。

示例数据将完成游戏。他们将拥有以下数据:
用户ID
完成时间
游戏数据
等等……

随后的查询将按 UserId 查找所有已完成的游戏。我们希望确保以最佳方式收集数据以供以后查询,而不是稍后进行重构。

【问题讨论】:

    标签: firebase react-native react-native-firebase


    【解决方案1】:

    就您而言,首先要确保您有充分的理由使用 Firebase 而不是 Firestore。一旦您确定应该坚持使用 Firebase 实时数据库,请查看以下文档摘录。因此,您实际上可能有 2 个单独的父节点,1 个用于 userId,另一个用于游戏。每个游戏节点的子节点都是一个特定的游戏,它有一个游戏用户的子树(按 userId)。

    Flatten data structures 如果数据被拆分为单独的路径,也称为 非规范化,它可以在单独的调用中有效地下载, 因为它是需要的。考虑这种扁平结构:

    {
      // 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": { ... }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多