在Firebase数据库中存储用户的正确方法 [英] Correct Way To Store Users In Firebase Database

查看:106
本文介绍了在Firebase数据库中存储用户的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在为我的Firebase数据库开发数据库布局。我正在通过许多不同的教程来了解布局应该如何看,但是我对于什么是正确的有些困惑。



我不确定用户应该存储为,让我给你一个例子:

  {
users:{
alanisawesome:{
nickname:Alan The Machine
},
gracehop:{
nickname:Amazing Grace
}






在上面的示例中,他们正在存储Firebase Legacy文档用户通过他们的用户名。在另一个例子中,我发现了以下内容:

  {
users:{
123 :{
name:Kato
},
234:{
name:Anant
}
}

这里他们用UID存储用户。那么你可以简单地只用 push()新用户,他们的ID是随机键,如下面的 -JRHTHaKuITFIhnj02kE

以上哪一项是存储用户的最佳方式?用户名,自定义的UID或者随机生成的密钥? 解决方案

你想要按时间顺序排列,哪些项目没有一个自然的关键。所以一个博客文章,聊天消息等列表。

但是当项目有一个自然的关键,它通常是更好的存储在这个自然的关键。用户有一个唯一的ID,通常被称为他们的 uid 。除非你有理由不这样做,否则将用户存储在 uid 下。通过将它们存储在它们的 uid 下,你可以很容易地查找给定用户的数据: ref.child('users')。child(user .uid)。如果您将用户存储在生成的密钥(例如推送ID)下,则必须执行查询以查找相同的记录。



您得到的示例从文档中使用不同的命名方案。这样做的原因是推送ID和UID都会导致比我们想要的文档更加混乱。这就是为什么我们在这些情况下选择了简短易读的关键。


currently I am working on a Database Layout for my Firebase Database. I am looking through many different tutorials on how the layouts should look, but I am getting a bit confused on what is proper.

I am not sure on what the users should be stored as, let me give you an example:

{
  "users": {
    "alanisawesome": {
      "nickname": "Alan The Machine"
    },
    "gracehop": {
      "nickname": "Amazing Grace"
    }
  }
}

In the example above from the Firebase Legacy docs they are storing users by their Usernames. Yet in another example I found the following:

{
  "users": {
    "123": {
      "name": "Kato"
    },
    "234": {
      "name": "Anant"
    }
  }
}

Here they are storing the users with a UID. Then you can simply just push() new users, and their IDs are random keys such as the following -JRHTHaKuITFIhnj02kE.

Which of the above is the best way to store users? Username, Custom made UID, or random generated Key?

解决方案

Firebase push ids are great when you have a collection that you want to order chronologically, where the individual items don't have a natural key. So a list of blog posts, chat messages, etc.

But when items have a natural key, it is usually better to store them under that natural key. Users have a unique id, usually referred to as their uid. Unless you have a reason not to do so, store the users under their uid. By storing them under their uid, you can easily look up the data for a given user: ref.child('users').child(user.uid). If you stored the users under a generated key (such as a push id), you'd have to perform a query to find the same record.

The examples you got from the documentation are using a different naming scheme. The reason for that is that both push IDs and UIDs would have led to more confusion than we wanted in the docs at that point. That's why we chose short, readable keys in those cases.

这篇关于在Firebase数据库中存储用户的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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