【发布时间】:2021-08-16 16:45:35
【问题描述】:
你好美丽的社区。希望你们一切都好。
所以我正在开发这个应用程序,其中有两个 MongoDB 集合,Posts 和 CommentsView。
Posts 集合由post title、post 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