【问题标题】:MongoDB/Mongoose: How can I count documents in one collection and add it to another based on id?MongoDB/Mongoose:如何计算一个集合中的文档并根据 id 将其添加到另一个集合中?
【发布时间】:2021-08-16 16:45:35
【问题描述】:

你好美丽的社区。希望你们一切都好。

所以我正在开发这个应用程序,其中有两个 MongoDB 集合,PostsCommentsView

Posts 集合由post titlepost type 和它们的comments 组成。 comments 是一个对象数组,由每个评论的查看次数和 id 组成。

CommentsView 集合中,我打算在查看所有cmets 时存储它们。重复不是问题。

架构如下所示:

帖子架构:

const postsSchema = new mongoose.Schema( {
    postTitle: {
        type: String
    },
    comments: [ {
        totalViews: {
            type: Number
        },
        uid: {
            type: String
        }
    }],
    postId: {
        type: String
    }
} );

评论视图架构:

const commentsViewSchema = new mongoose.Schema( {
    text: {
        type: String,
    },
    uid: {
        type: String
    }
} );

假设我有一个帖子,其中有两个 uid 分别为“A”和“B”的 cmets。每当查看带有 uid 'A' 的评论时,我都会在 CommentsView 集合中创建评论。并自动在Posts 集合的totalView 字段中添加1 个视图。当再次查看相同的评论时,我会先将其添加到 CommentsView 集合中,然后在 Posts 集合中增加 totalView 字段。

假设我在 Comments 集合中有这些文档:

{
    text: 'Life is good',
    uid: 'A'
},
{
    text: 'Boom Boom',
    uid: 'B'
},
{
    text: 'Bam Bam',
    uid: 'A'
},

因此 Posts 文档将如下所示:

{
    postTile: '60 seconds to Mars',
    comments: [
        {
             uid: 'A',
             totalViews: 2,
        },
        {
             uid: 'B',
             totalViews: 1,
        },
    ],
    postId: '1s93njs'    
}

我还没有尝试过任何东西,因为我不知道从哪里开始。看起来好复杂。

如果我希望整个过程自动化,我该如何实现?

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    由于您使用两种不同的架构,我建议您使用引用, 因为如果您想添加更多功能,例如点赞数和线程数,那么下面建议的此模型将很有用

    不要使用 String 类型来存储模型的 Id,使用 mongoose 模块提供的类型 ObjectId

    后模型

    const postsSchema = new mongoose.Schema( {
    postTitle: {
        type: String
    },
    comments: [ 
        type:ObjectId,
        ref:"model name"
    ],
        postId: {
          type: String
        }
    }, {timestamps:true});
    

    评论模型

     const commentsViewSchema = new mongoose.Schema( {
           text: {
             type: String,
             required:true
           },
           totalView:{
              type: Number,
              default:0
           }
      },{timestamps:true} );
    

    现在每次查看帖子时,您都可以搜索评论 ID 并增加文档中的计数。如果您填充数组中的所有 cmets,所有内容都将反映在 post 模型中

    示例

    工作流程

    用户查看了评论-->(getcomment_id)-->更新评论

    现在所有后续请求包含此评论的帖子也将具有更新的视图,因为参考。

    查询以更新 cmets

    comment.findByIdAndUpdate({_id:}, {$inc:{totalView:1}})

    另一种方法是嵌入do,如果你认为帖子不会有太多的cmets,我推荐这个,否则有上面的模型很好。

    您可以进一步参考这些文章

    rules to follow for designing mongodb schema

    希望这可以帮助您获得一个好主意

    【讨论】:

    • 嘿伙计。感谢你的回答。但遗憾的是,这不是我想要的。让我再给你解释一下。 posts 模型创建一个帖子,每当用户发表评论时,它将作为对象添加到 cmets 数组中。我没有在 OP 中提到 Comments 模型,因为 Comments 模型与 CommentsView 模型是分开的。
    • CommentsView 模型只会在查看评论时创建文档。 100 万人可以查看 1 条评论。我还需要存储那数百万人的 IP。所以重复不是问题。我只需要在 CommentsView 中计算这 100 万个文档,然后通过 id 找到它并将其存储在 Posts 的 cmets 数组中。
    猜你喜欢
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    相关资源
    最近更新 更多