Node.js Mongoose-如何使用预钩子移除级联删除
我有一个矩模式,它可以有多个相关联的注释。当某个时刻被删除时,我希望所有与该时刻相关的评论也被删除 动量图式Node.js Mongoose-如何使用预钩子移除级联删除,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,我有一个矩模式,它可以有多个相关联的注释。当某个时刻被删除时,我希望所有与该时刻相关的评论也被删除 动量图式 let MomentSchema = new Schema({ body: String, likes: [{ type: Schema.Types.ObjectId, ref: 'User', }], dislikes: [{ type: Schema.Types.ObjectId, re
let MomentSchema = new Schema({
body: String,
likes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
dislikes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment',
}],
author: {
type: Schema.Types.ObjectId,
ref: 'User',
},
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'},
});
MomentSchema.pre('remove', (next) => {
//What to include here to delete all the comments for this moment
});
let CommentSchema = new Schema({
body: {
type: String,
required: true,
},
moment: {
type: Schema.Types.ObjectId,
ref: 'Moment',
},
author: {
type: Schema.Types.ObjectId, ref: 'User',
required: true,
},
likes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
dislikes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
parent: {
type: Schema.Types.ObjectId,
ref: 'Comment',
},
replies: [{
type: Schema.Types.ObjectId,
ref: 'Comment',
}],
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'},
});
仅供参考,一条评论也可以有一个回复,这也是一个评论模型,因此我还需要在评论模型中添加一个pre-remove钩子
注释模式
let MomentSchema = new Schema({
body: String,
likes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
dislikes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment',
}],
author: {
type: Schema.Types.ObjectId,
ref: 'User',
},
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'},
});
MomentSchema.pre('remove', (next) => {
//What to include here to delete all the comments for this moment
});
let CommentSchema = new Schema({
body: {
type: String,
required: true,
},
moment: {
type: Schema.Types.ObjectId,
ref: 'Moment',
},
author: {
type: Schema.Types.ObjectId, ref: 'User',
required: true,
},
likes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
dislikes: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
parent: {
type: Schema.Types.ObjectId,
ref: 'Comment',
},
replies: [{
type: Schema.Types.ObjectId,
ref: 'Comment',
}],
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'},
});
API删除:时刻/:id
destroy(req, res) {
let id = req.params.id;
Moment.findOne({'_id': id}, (err, moment) => {
if (err) {
return res.status(404).json({
success: false,
status: 404,
data: {},
message: 'Failed to find moment',
});
}
moment.remove((err, moment) => {
if (err) {
return res.status(400).json({
success: false,
status: 400,
data: err,
message: 'Failed to delete moment',
});
}
return res.status(200).json({
success: true,
status: 200,
data: {},
message: 'Successfully deleted moment',
});
});
});
}
我需要在pre-hook中删除什么才能删除与该时刻相关的所有注释。此外,当在预钩子移除中调用“this”时,它返回未定义。因此要移除所有注释和多级级联,我必须调用文档上的.remove方法。将下面的代码插入力矩预钩移除函数,它将在移除力矩之前删除注释 此外,在删除注释之前,它将调用注释前钩子。在这里,我可以添加代码来删除对每条评论的回复
Comment.find({'moment': this._id})
.then((comments) => {
Promise.all(comments.forEach((comment) => comment.remove()))
.then(next());
});
此处使用了promise.all,因为我们只想在删除所有注释后删除该时刻,因此要删除所有注释和多级级联,我必须调用文档上的.remove方法。将下面的代码插入力矩预钩移除函数,它将在移除力矩之前删除注释 此外,在删除注释之前,它将调用注释前钩子。在这里,我可以添加代码来删除对每条评论的回复
Comment.find({'moment': this._id})
.then((comments) => {
Promise.all(comments.forEach((comment) => comment.remove()))
.then(next());
});
此处使用的是promise.all,因为我们只想在删除所有评论后删除该时刻