[英]Firestore Posts & Comments Data Model
Coming from an RDMS background I am trying to understand NoSQL databases and planning a simple project that includes topics, posts & comments.来自 RDMS 背景,我试图理解 NoSQL 数据库并计划一个包括主题、帖子和评论的简单项目。
Topics have posts & posts have comments主题有帖子,帖子有评论
I have found the following guide that suggests using the following top-level collections:我发现以下指南建议使用以下顶级 collections:
https://firebaseopensource.com/projects/firebase/quickstart-android/database/readme/ https://firebaseopensource.com/projects/firebase/quickstart-android/database/readme/
I fail to understand the benefits of (3) above as surely we can simply filter (2) based on the user, even 3 would still need to be filtered.我无法理解上面 (3) 的好处,因为我们当然可以根据用户简单地过滤 (2),即使是 3 仍然需要过滤。
What is the logic of having comments as a top-level collection as opposed to having comments as a subcollection under posts?将评论作为顶级集合而不是将评论作为帖子下的子集合的逻辑是什么? Is this not the better way to store hierarchical data?
这不是存储分层数据的更好方法吗?
In the NoSQL world, we are structuring a database according to the queries that we want to perform.在 NoSQL 世界中,我们正在根据要执行的查询构建数据库。
What is the logic of having comments as a top-level collection as opposed to having comments as a subcollection under posts?
将评论作为顶级集合而不是将评论作为帖子下的子集合的逻辑是什么?
None is better than the other.没有一个比另一个更好。 However, there are some differences:
但是,存在一些差异:
Is this not the better way to store hierarchical data?
这不是存储分层数据的更好方法吗?
There is no "perfect", "the best" or "the correct" solution for structuring a Cloud Firestore database.构建 Cloud Firestore 数据库没有“完美”、“最佳”或“正确”的解决方案。 We always choose to create a structure for our database that satisfies our queries.
我们总是选择为我们的数据库创建一个结构来满足我们的查询。 So in your case, I would create a schema that looks like this:
所以在你的情况下,我会创建一个看起来像这样的模式:
Firestore-root
|
--- users (collection)
| |
| --- $uid (document)
| |
| --- //user fields.
|
--- posts (collection)
|
--- $postId (document)
|
--- uid: "veryLongUid"
|
--- //user fields.
|
--- comments (sub-collection)
|
--- $commentId (document)
|
--- uid: "veryLongUid"
|
--- //comment fields.
Using this schema you can:使用此架构,您可以:
Am I missing something?我错过了什么吗?
If you think that all the comments of a post might fit into 1 MiB maximum limitation , then you should consider adding all comments into an array.如果您认为一篇帖子的所有评论都可能符合1 MiB 的最大限制,那么您应该考虑将所有评论添加到一个数组中。 If not, I highly recommend you read the following approach:
如果没有,我强烈建议您阅读以下方法:
Where I have explained how can we store up to billions of comments and replies in Firestore.我已经解释了我们如何在 Firestore 中存储多达数十亿条评论和回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.