如何使用Mongoose和ExpressJ删除MongoDB中的所有相关文档?"/>
如何使用Mongoose和ExpressJ删除MongoDB中的所有相关文档?
当用户删除博客帖子时,我试图删除对该博客帖子的所有评论和回复。我尝试了中间件,但它们非常混乱,似乎无法正常工作。有什么方法可以添加删除中间件,该中间件每次都被删除,无论删除博客的方法是什么。
这是我所有的模式-
博客帖子架构
var blogSchema = mongoose.Schema({
name: { type: String, minlength: 3, maxlength: 256},
body: { type: String},
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'comment'
}],
author: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'user'
}
}, {timestamps: true});
注释模式
var commentSchema = mongoose.Schema({
body: { type: String, maxLength: 500 },
replies: [{
type: mongoose.Schema.Types.ObjectId,
refPath: 'reply'
}],
author: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'user'
},
target: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'blog'
}
}, {timestamps: true});
回复模式
var replySchema = mongoose.Schema({
body: { type: String, maxLength: 500 },
author: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'user'
},
target: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'comment'
}
}, {timestamps: true});
回答如下:您可以使用Ajax完成。 ==> Cannot delete article in Node/JavaScript app之类的其他内容可能对您有帮助。您可以根据需要更改codejs。
更多推荐
如何使用Mongoose和ExpressJ删除MongoDB中的所有相关文档?
发布评论