【问题标题】:MongoDB schema: Like a Comment OR a PostMongoDB 模式:喜欢评论或帖子
【发布时间】:2021-10-09 03:05:31
【问题描述】:

我想在我的应用中设置一个“赞”系统。用户应该能够喜欢帖子或评论(当然是帖子的评论)。我应该如何设计这个?

用户

const userSchema = new Schema({
    id: { type: String, required: true },
    username: { type: String, required: true },
    password: { type: String, required: true },
});

帖子

const postSchema = new Schema({
    content: { type: String, required: true },
    authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true }
});

评论

const commentSchema = new Schema({
    content: { type: String, required: true },
    authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
    postId: { type: mongoose.Schema.Types.ObjectId, ref: "Post", required: true },
});

喜欢

const likeSchema = new Schema({
    content: { type: String, required: false },
    authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
    postId: { type: mongoose.Schema.Types.ObjectId, ref: "Post", required: function() { return this.commentId? false : true } },
    commentId: { type: mongoose.Schema.Types.ObjectId, ref: "Comment", required: function() { return this.postId? false : true } }
});

我来自关系数据库,也许我的设计对于 nosql 是完全错误的。我的主要询问是关于喜欢的,我不知道如何接受对帖子或评论的喜欢。

【问题讨论】:

  • 为什么不在帖子中嵌入评论?

标签: mongodb mongoose


【解决方案1】:

我更喜欢单独的收藏:

User:
    id:  
    ...

Post:
    id:
    userId:
    ...

Comment:
    id:
    userId:
    postId:

Like:
    id:
    userId:
    postId:
    commentId:

第二个存储数组将导致您在后端循环依赖。尤其是当你使用 NodeJS 和严格流动时。

【讨论】:

    【解决方案2】:

    MongoDB 在存储文档方面非常强大。文件保存关系。 我将以访问您的数据的方式对其进行建模。我确实建议使用强大的聚合框架和数组运算符来体验这些可能性。我要探索的是以下内容

    User:
      id: 
      name:
      picture:
      ...
    
    Posts:
      id: 
      authorid:
      content:
      total_views:
      tags: array of String
      likes: array of Likes {[
             liked_by: user_id
         ],...}
      comments: array of Comments {[
         author_id: ...
         comment: ...
         reactions: array of Comments {[],...}
         likes: array of Likes {[
             liked_by: user_id
              ],...}
          ],...}
    

    这个模型可以扩展吗?文档可以容纳 16MB 的数据。 16MB 的文本格式是巨大的。

    PS 请重新考虑在数据库中存储用户名/密码。这是一个完全不同的讨论。查看身份验证、授权、OAuth、哈希/加盐等主题。

    【讨论】:

    • 当然我会在将密码存储到数据库之前使用哈希/加盐=)这只是stackoverflow的简化版本。
    【解决方案3】:
    post={
     ...keys,
     likes:[likeSchema],
     comments:[CommentSchema]
    }
    

    这是我更喜欢的,即使你想存储递归 cmets 也可以使用

    commentschema={
     id:unique commet id
     text:...
     user_id:who wrote this comment
     parent_id: to which this comment belongs to!
     depth: comment depth as your wish (mostly 2)
    }
    

    对于直接在帖子上发布的评论,父 ID 将为空 parent id 将是该评论发布到的评论的comment_id。如果是递归注释。

    希望你能明白。

    【讨论】:

      【解决方案4】:

      因为,问题是关于评论或帖子的架构。我会关注点赞数。

      构建这样的架构。这里的 targetId 将是 postId 或 commentId

      const likeSchema = new Schema({
          content: { type: String, required: false },
          authorId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
          targetId: { type: mongoose.Schema.Types.ObjectId, ref: "Post", required: function() { return this.commentId? false : true } }
      });
      

      您需要考虑的几点:

      1. 在帖子收藏中存储喜欢的帖子
      2. 在 cmets 集合中存储喜欢的 cmets
      3. 您需要建立一种机制来计算喜欢并存储在该集合中

      【讨论】:

        猜你喜欢
        • 2015-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-26
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多