【问题标题】:How to update a Firebase child without 'knowing' the parents?如何在不“知道”父母的情况下更新 Firebase 孩子?
【发布时间】:2023-03-04 22:44:01
【问题描述】:

假设我在 Firebase 数据库中有以下数据。需要数据来快速查看特定游戏中的 2 名玩家。

现在我想通过一次查询来更新 John Doe 的姓名(用户 ID = UID-1)。理想情况下,有一种方法可以做这样的事情。显然这是伪代码... :-)

Firebase.ref('/game-players/*/UID-1').update("John with a different Doe")

"game-players" : {
  "game-1" : {
    "UID-1" : "John Doe",
    "UID-2" : "Donald Trump"
  },
  "game-2" : {
    "UID-1" : "John Doe",
    "UID-3" : "Barrack Obama"
  }
}

这可以通过首先检查玩家参与的所有游戏来解决(因为该数据在树的不同分支中也可用),但我不喜欢必须基于生成大量不同查询的想法在那个数据上。

有什么想法可以让我朝着正确的方向前进吗?

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    你应该改变你的数据结构。

    players:
    |--uidxx
    |---- name
    |---- surname
    |---- ...
    |--uidx2
    |---- name
    |---- surname
    |---- ...
    
    game-players :
    |-- game-1 : 
    |----   uidxx : true,
    |----   uidx2 : true,
    

    players 节点中,您可以将所有玩家与数据(例如名称)放在一起。
    gameplayers 节点中,您仅使用玩家的uid 而不是数据。
    这样你只需要更新玩家节点中的数据,而不需要查询所有游戏。

    【讨论】:

    • 嗯...但是对于非规范化和“数据结构遵循您的用例”这不是矛盾的吗?我的意思是:当我想打印一个包含每个游戏所有用户名的列表时,我现在需要执行 2 个查询...
    • 是这样的扁平化数据:firebase.google.com/docs/database/android/…
    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多