【发布时间】: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 是完全错误的。我的主要询问是关于喜欢的,我不知道如何接受对帖子或评论的喜欢。
【问题讨论】:
-
为什么不在帖子中嵌入评论?