帖子的引用评论"/>
猫鼬删除所有带帖子的引用评论
这是我的帖子架构。其中包含参考注释的集合。
const postSchema = new mongoose.Schema({
title: String,
content: String,
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}]
});
const Post = mongoose.model("Post", postSchema);
这是我的评论架构。
const commentSchema = new mongoose.Schema({
text: String
});
const Comment = mongoose.model("Comment", commentSchema);
这是我删除帖子的代码。
Post.findByIdAndDelete(req.params.id, err => {
if (!err) {
res.redirect("/posts");
} else {
console.log(err);
res.redirect("back");
}
});
现在,我想删除所有引用此帖子的评论。现在我该怎么办。
回答如下:这里我将deleteMany
与$in
运算符一起使用。
const deleted = await Post.findByIdAndDelete(req.params.id);
await Comment.deleteMany({_id: {$in: deletedments}});
更多推荐
猫鼬删除所有带帖子的引用评论
发布评论