在Firebase数据库中嵌套星星 [英] Nesting stars in Firebase Database

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

问题描述

我发现Firebase数据库非常有帮助,但是我注意到了Firebase数据库示例这让我担心一些。



我的意思是在这个例子中,用户可以给帖子加星标,就像Facebook上的Like it一样。在提供的示例中,它们嵌套在post中,所以我们有这样的示例对象:

$ p $ $ $ $ $ $ $ $ $ $ $ $ author:username,
body:一些内容,
starCount:1
stars:{
user_id_who_gave_star:true
}
title:Some title,
uid:author_id
}

这样的解决方案有很多优点,例如我们可以检查是否已经给星和隐藏或更改图标,或者我们可以通过一个事务来改变starCount,并添加下一个值到stars。

但是问题是,当我们有大的应用程序和1000名用户给明星,所以每次我们下载邮政数据时,我们下载1000个用户ID可能不是最好的解决方案。

我的问题是,这样的应用程序的最佳方法是什么,并有人测试过Firebase在这种情况下的工作方式?
$ b 在这里为我们的文档编写例子时,我们总是会使用这个例子来解决这个问题。不得不平衡的需要有足够的上下文,保持足够小的例子,并遵循我们自己的最佳做法。

这确实是其中之一,我们违反我们自己的最佳实践是不嵌套数据。正如你所说:这意味着一个用户下载一个帖子,获取喜欢该帖子的用户的所有UID。

由于应用程序的使用规模,这可能成为一个问题。如果您的应用程序属于这种情况,那么您应该将投稿的用户建模为单独的顶级节点:

 posts:{
post_id:{
author:username,
body:Some content,
starCount:1
title:Some title,
uid:author_id
}
},
upvotes:{
post_id :{
user_id_who_gave_star:true
}
}


I find Firebase Database sample very helpful, but I noticed something which worries me a little bit.

I mean in this example user can give star to the post, something like "Like it" on Facebook. In provided sample they nested stars into post, so we have sample object like this:

"post_id" : {
       "author": "username",
       "body": "Some content",
       "starCount": 1
       "stars" : {
              "user_id_who_gave_star" : "true"
       }
      "title": "Some title",
      "uid": "author_id"
}

Such solution has many advantages, like e.g. we can check if have already gave star and hide or change icon, or we can one transaction to change "starCount" and add next value to "stars".

But the problem is when we have big application and 1000 users gave star, so everytime when we download post data we download 1000 userIds which may be not best solution.

Question

My question is, what is best approach for such applications and have someone tested how Firebase works in this situation?

解决方案

firebaser here

When writing the examples for our documentation, we always have to balance the need for having enough context, keeping the example small enough , and following our own best practices.

This is indeed one of the cases where we violate one of our own best practices "not nesting data". As you said: this means that a user downloading a post, gets all the UIDs of users that liked that post.

As the usage of the application scales, that may become a concern. If that is the case for your app, you should model the "users who upvoted a post" as a separate top-level node:

"posts": {
    "post_id" : {
       "author": "username",
       "body": "Some content",
       "starCount": 1
      "title": "Some title",
      "uid": "author_id"
    }
},
"upvotes": {
    "post_id" : {
        "user_id_who_gave_star" : "true"
    }
}

这篇关于在Firebase数据库中嵌套星星的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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