您将如何在 Firebase 中为用户和朋友的集合建模? [英] How would you model a collection of users and friends in Firebase?

查看:9
本文介绍了您将如何在 Firebase 中为用户和朋友的集合建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Firebase 创建一个数据库 (json).我搜索了文档和网络,但找不到明确的开始方式.我想要一个用户数据库.每个用户(表示为 UID)都应该有一个昵称和一个朋友列表.

I'm trying to create a database (json) with Firebase. I searched the docs and the net but couldn't find a clear way to start. I want to have a database of users. each user (represented as UID) should have a nickname and a list of friends.

我尝试制作一个如下所示的 .json 文件:

I tried making a .json file that looks like this:

{
  users:{

  }
}

并将其添加到 Firebase 控制台以开始使用,但它不起作用.我该怎么做?

and adding it to the Firebase console to get started but it wouldn't work. How can I do it?

数据库应如下所示:

{
  users:{
       UID:{
          nickname: hello
          friends: UID2
       }
       UID2:{
          nickname: world
          friends: UID
  }
}

我不知道我是否正确,所以我非常感谢你们在这个主题上给我的任何帮助.提前致谢!

I don't know if I got that right, so I would really appreciate any help you guys could give me at this subject. Thanks in advance!

推荐答案

似乎是一个不错的起点.不过我会做两处改变.

Seems like a good place to start. I would make two changes though.

  1. 将列表与好友分开
  2. 将朋友作为一个集合,而不是单个值或数组

保持列表是朋友分开的

使用 Firebase 数据库时的基本建议是保持数据结构浅/平坦.这有很多原因,你至少有两个.

keep the list is friends separate

A basic recommendation when using the Firebase Database is to keep your data structure shallow/flat. There are many reasons for this, and you have at least two.

  1. 使用您当前的数据结构,假设您要显示用户名列表.您只能通过收听 /users 获得该列表.这意味着您不仅可以获得每个用户的用户名,还可以获得他们的朋友列表.您向用户显示所有数据的可能性很小,这意味着您只是浪费了他们的一些带宽.

  1. With your current data structure, say that you want to show a list of user names. You can only get that list by listening to /users. And that means you don't just get the user name for each user, but also their list of friends. Chances that you're going to show all that data to the user are minimal, so that means that you've just wasted some of their bandwidth.

假设您想让每个人都可以阅读用户名列表.但是您只希望每个用户能够阅读他们自己的朋友列表.您当前的数据结构很难做到这一点,因为 权限级联规则不是过滤器.

Say that you want to allow everyone to read the list of user names. But you only want each user to be able to read their own list of friends. Your current data structure makes that hard, since permission cascades and rules are not filters.

更好的结构是将用户个人资料列表(目前只是他们的姓名)与每个用户的朋友列表分开.

A better structure is to keep the list of user profiles (currently just their name) separate from the list of friends for each user.

friends 属性当前只有一个值.当您开始构建应用程序时,您将需要存储多个朋友.最常见的是然后存储一个数组或 UIDS 列表:

You current have just a single value for the friends property. As you start building the app you will need to store multiple friends. The most common is to then store an array or list of UIDS:

[ UID1, UID2, UID3 ]

或者

{
  "-K.......1": "UID1"
  "-K.......5": "UID2"
  "-K.......9": "UID3"
}

不幸的是,这些数据结构的类型错误.数组和第二个集合都是列表:(可能)非唯一值的有序集合.但是朋友的集合不一定是有序的,它必须是唯一的.我要么在集合中,要么不在集合中,我不能多次在集合中,并且顺序通常无关紧要.这就是为什么您经常使用上述模型寻找 friends.contains("UID1")ref.orderByValue().equalTo("UID1") 操作的原因.

These are unfortunately the wrong type for this data structure. Both the array and the second collection are lists: an ordered collection of (potentially) non-unique values. But a collection of friends doesn't have to be ordered, it has to be unique. I'm either in the collection or I'm not in there, I can't be in there multiple times and the order typically doesn't matter. That's why you often end up looking for friends.contains("UID1") or ref.orderByValue().equalTo("UID1") operations with the above models.

一个更好的模型是将数据存储为一个集合.集合是无序值的集合,它们必须是唯一的.非常适合朋友的集合.要将其存储在 Firebase 中,我们使用 UID 作为集合的键.由于我们不能存储没有值的键,所以我们使用 true 作为虚拟值.

A much better model is to store the data as a set. A set is a collection of unordered values, which have to be unique. Perfect for a collection of friends. To store that in Firebase, we use the UID as the key of the collection. And since we can't store a key without a value, we use true as the dummy value.

所以这导致了这个数据模型:

So this leads to this data model:

{
  users:{
     UID:{
       nickname: hello
     }
     UID2:{
       nickname: world
     }
  }
  friends:{
     UID:{
       UID2: true
     }
     UID2:{
       UID: true
     }
  }
}

关于 NoSQL 数据建模,特别是 Firebase,还有很多话要说/了解.要了解这一点,我建议阅读 NoSQL 数据建模 并观看 面向 SQL 开发人员的 Firebase.

There is a lot more to say/learn about NoSQL data modeling in general and Firebase specifically. To learn about that, I recommend reading NoSQL data modeling and watching Firebase for SQL developers.

这篇关于您将如何在 Firebase 中为用户和朋友的集合建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆