简体   繁体   English

Firestore 帖子和评论数据 Model

[英]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:

  1. A users collection用户集合
  2. A posts collection帖子集合
  3. A user-posts collection用户帖子集合
  4. A posts-comments collection一个帖子评论集合

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:使用此架构,您可以:

  • Get all users.获取所有用户。
  • Get all posts in the database.获取数据库中的所有帖子。
  • Get all posts that correspond to only a particular user.获取仅对应于特定用户的所有帖子。
  • Get all comments of all posts, of all users in the database.获取数据库中所有用户的所有帖子的所有评论。 Requires a collection group query .需要集合组查询
  • Get all comments of all posts that correspond to a particular user.获取与特定用户相对应的所有帖子的所有评论。 Requires a collection group query.需要集合组查询。
  • Get all comments of all users that correspond to a particular post.获取与特定帖子相对应的所有用户的所有评论。
  • Get all comments of a particular user that correspond to a particular post.获取与特定帖子相对应的特定用户的所有评论。

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.

相关问题 Firestore 数据到 Model class - Firestore data into a Model class 更改现有云 Firestore 集合中的数据 model? - Changing data model in existing cloud firestore collection? 用于活动策划应用程序的 Firestore 数据 model - Firestore data model for events planning app Firestore 社交媒体好友请求数据 model - Firestore social media friend requests data model Typescript React + Firestore 嵌套评论优化 - Typescript React + Firestore Nested Comments Optimisation 在 FireStore 中删除评论的最安全且成本更低的方法 - Safest and less expensive way to delete comments in FireStore 如何通过 createdAt 在 firebase firestore 中订购评论 - How to order comments in firebase firestore by createdAt 我可以强制 Firestore 文档具有特定的类型或数据 model(即 static 类型)吗? - Can I force a Firestore document to have a specific Type or data model (i.e. static typing)? Firebase:如果我在 Firestore 集合中查询一条记录并将一列数据传递到 model,我的应用程序是否会对下一个 model 进行第二次查询? - Firebase: If I query a Firestore collection for a record and pass one column of data into a model, would my app do a second query for the next model? Cloud Firestore:model 会员邀请 - Cloud Firestore: model membership invites
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM