firebase - 为什么在对数据进行非规范化时使用对象?

标签 firebase

在最近的博客文章 denormalising data 中,它建议在每个用户下面记录所有用户的评论,如下所示:

comments: {
    comment1: true,
    comment2: true
}

为什么这不是这样的列表:

comments: [
    "comment1",
    "comment2",
]

有什么优点?有什么区别吗?当我这样做时,您将如何为分布式应用程序的这些评论生成唯一的引用?我想象通过一个列表,我只需将它们推到末尾并让数组处理索引。

最佳答案

Firebase 只存储对象。 JS 客户端使用索引作为键将数组转换为对象。因此,例如,如果您使用 set 存储以下数组:

comments: [
  "comment1",
  "comment2"
]

在 Forge(图形调试器)中,它将显示为:

comments:
   0: comment1
   1: comment2

鉴于此,直接将评论 ID 存储为键的优点是您可以直接在安全规则中引用它,例如使用如下表达式:

root.child('comments').hasChild($comment)

为了生成这些评论的唯一引用,请使用 push ( https://www.firebase.com/docs/managing-lists.html ):

var commentsRef = new Firebase("https://<example>.firebaseio.com/comments");
var id = commentsRef.push({content: "Hello world!", author: "Alice"});
console.log(id); // Unique identifier for the comment just added.

关于firebase - 为什么在对数据进行非规范化时使用对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072494/

相关文章:

javascript - Firebase Cloud Function database.ServerValue.TIMESTAMP 不返回时间戳

java - 从 Firebase Firestore 将文档的一部分获取到对象中

javascript - 如何在 Javascript 中使用 userID 查询 Firebase 中的数据?

java - 上传到 FirebaseStorage 之前进行图像压缩

firebase - Firebase 数据库是否提供事件、触发器和存储过程等功能?

ios - 使用 Swift 3 时的“InvalidPathValidation”

swift - 计算评级给出 "NaN"

firebase - 如何仅在使用 onUpdate 触发器 Firestore 云功能更改字段时触发操作?

android - 应用程序启动时的 Firebase 需要超过 3 秒才能加载数据

firebase - Flutter - 可以在没有 firebase 的情况下使用 Google 登录吗?