社交媒体帖子MongoDB模式设计建议



我一直在使用MongoDB为我的应用程序,但想添加一个社交媒体帖子功能到系统中。然而,考虑到需求,我发现想出一个对我的用例有效的MongoDB模式并不容易。这里有专家愿意分享他们的建议吗?

要求如下:

  1. 用户可以发布社交媒体帖子(想象一下instagram帖子)
  2. 用户将查看其他用户的社交媒体帖子
  3. 当一个帖子在用户屏幕上显示5秒时,我们将记录用户已经看到该帖子
  4. 被看到的帖子将出现在用户的社交媒体提要中

任何帮助将不胜感激,谢谢!

您可以参考下面的模式,我们需要管理用户和他们的帖子以及帖子历史,如(谁喜欢,谁观看和谁添加评论)


// Users schema
const userSchema = new Schema({
name: {
type: String,
trim: true
}
});
// Post related media schema
const postMediaSchema = new Schema({
url: {
type: String,
trim: true
}
}, {
_id: false
})
// Post schema
const postSchema = new Schema({
userId: {
type: Schema.Types.ObjectId,
ref:'users',
},
title: {
type: String,
trim: true
},
description: {
type: String,
trim: true
},
media: [postMediaSchema],
tags: [String]
});
// Users and posts relation schema 
const postWatchHistory = new Schema({
userId: {
type: Schema.Types.ObjectId,
ref:'users',
},
postId: {
type: Schema.Types.ObjectId,
ref:'posts',
},
});
// Post liked history schema
const postLikedHistory = new Schema({
userId: {
type: Schema.Types.ObjectId,
ref:'users',
},
postId: {
type: Schema.Types.ObjectId,
ref:'posts',
},
});
// Post comment history schema
const postCommentHistory = new Schema({
userId: {
type: Schema.Types.ObjectId,
ref:'users',
},
postId: {
type: Schema.Types.ObjectId,
ref:'posts',
},
text:{
type: String,
required: true,
trim: true
},
status:{
type: String,
enum:['Y', 'N'],
default:'N',
trim: true
}
});

最新更新