结构化我的数据 - Firebase [英] Structuring my data - Firebase
问题描述
我正在创建一个原型组列表应用程序。我想要以下对象:
- 用户
- 列表
p>
http://myapp.firebase.io/user/
http://myapp.firebase.io/user/ uid / lists /
http://myapp.firebase.io/list/
http://myapp.firebase.io/item/listid/
http://myapp.firebase。 io / comment / itemid
其中 http://myapp.firebase.io/user/uid/lists/ 指向列出唯一ID的 http://myapp.firebase.io/item/listid/ 指向给定列表的所有项目对象, http://myapp.firebase.io/comment/itemid 指向给定项目的所有注释。
这种结构是否有意义?我这样做的原因,而不是嵌套进一步(即 http://myapp.firebase.io / list / listid / item / 的项目和 http:// myapp .firebase.io / list / listid / item / itemid / comment 用于注释)是因为它在文档中说,每当你获取一个对象,你获取所有的孩子。有时(甚至大多数时候)我想获取列表的项目,但不是每个项目的评论。
在NoSQL数据库中,您应该为您的数据建模如何打算使用它。我强烈建议您阅读此有关NoSQL数据建模的文章
顶层结构似乎很好,并且不违反 Firebase推荐限制数据嵌套。但是有很多其他地方,你可能仍然犯错误(这是为什么这个问题是一个太宽泛的Stack Overflow,但我会尽力回答一些它)。
我将用户的列表分成单独的顶级节点:
/ userlists / $ uid / $ listid
这样 / users / $ uid
节点将只包含用户的配置文件信息,你可以便宜地显示用户列表。您甚至可以考虑将用户个人资料的最明显的方面拆分为另一个顶级节点,以使这样的列表的显示更便宜。
/ usernames / $ uid
在这种情况下,您将复制数据。但是存储(相对)便宜,并且对于更常见的数据读取进行优化是NoSQL数据库可以如此规模化的原因之一。
正如您可能注意到的,专注于显示用户名列表,检索用户的列表以及访问特定用户的配置文件。
在NoSQL数据库中,您应该为您的应用程序访问它的数据建模。我强烈建议您阅读此有关NoSQL数据建模的文章 。
之后,写出您的使用案例列表,看看如何最方便地访问它的数据。自然地反规范化,偶尔会复制数据,适合用例。使用多位置更新 a>将非规范化和重复的数据与其主要实体保持同步。
I'm creating a prototype group list application. I want the following objects:
- User
- List
- Item
- Comment
I think that I should structure this as follows:
http://myapp.firebase.io/user/
http://myapp.firebase.io/user/uid/lists/
http://myapp.firebase.io/list/
http://myapp.firebase.io/item/listid/
http://myapp.firebase.io/comment/itemid
where http://myapp.firebase.io/user/uid/lists/ points to list unique id's, http://myapp.firebase.io/item/listid/ points to all item objects for a given list, and http://myapp.firebase.io/comment/itemid points to all comments for a given item.
Does this structure make sense? The reason I did it this way instead of nesting further (i.e. http://myapp.firebase.io/list/listid/item/ for items and http://myapp.firebase.io/list/listid/item/itemid/comment for comments) is because it says in the documentation that whenever you fetch an object you fetch all children. Sometimes (perhaps even most of the time) I want to fetch a list's items, but not each item's comments. I might only want to do that when a user clicks on the item.
In a NoSQL database you should model your data for how you intend to use it. I highly recommend reading this article on NoSQL data modeling.
The top-level structure seems fine and does not violate Firebase's recommendation to limit nesting of data. But there are many other places where you might still make mistakes (which is one of the reasons this question is a bit too broad for Stack Overflow, but I'll try to answer some of it anyway).
I'd separate out the user's lists into a separate top-level node:
/userlists/$uid/$listid
That way the /users/$uid
nodes would just contain the user's profile information and you could cheaply show a list of users. You might even consider splitting the most visible aspect of the user profile into another top-level node, to make the showing of such a list even cheaper.
/usernames/$uid
You'll be duplicating data in this case. But storage is (relatively) cheap, and optimizing for the more common reading of data is one of the reasons NoSQL databases can scale so well.
As you may notice, I focus on showing a list of user names, retrieving the lists for a user and accessing the profile for a specific user. These are use-cases and we're modeling the data to fit them.
In a NoSQL database you should model your data for how your app accesses it. I highly recommend reading this article on NoSQL data modeling.
After that, write out your list of use-cases and see how you can most easily access the data for it. Liberally denormalize and occasionally duplicate the data, to fit the use-cases. Use multi-location updates to keep denormalized and duplicated data in sync with its main entity.
这篇关于结构化我的数据 - Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!