MongoDB: 如何删除已删除post的所有评论?
MongoDB: How to delete all comments of a deleted post?
我正在关注 Thinkster 的 MEAN 堆栈教程 (https://thinkster.io/tutorials/mean-stack),其中并未说明应如何实施删除操作。
我目前有以下架构:
var PostSchema = new mongoose.Schema({
title: {type: String, required: true, maxlength: 140},
link: {type: String, required: true, maxlength: 300},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
var CommentSchema = new mongoose.Schema({
body: {type: String, maxlength: 200, minlength: 1},
post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' }
});
我有一个 Angular 工厂用于 CRUD 操作。我目前使用以下工厂方法删除 posts:
o.deletePost = function(id) {
return $http.delete('/posts/' + id, null, {}
}).success(function(res){
$window.location.href = '/#/home';
return res.data;
});
};
我的删除路由器如下所示:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.remove({_id: req.params.post}, function(err, post) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
不幸的是,当我删除这样的 post 时,任何相关的评论都会留在数据库中的某处。所以,我的问题是 如何在删除 post 的同时删除与 post 相关的所有评论?
我已经尝试 Google 寻找答案,看来我应该以某种方式使用 MongoDB 的中间件。我试过:
// Remove Post
module.exports.removePost = function(id, callback){
Post.findById(id, function (err, doc) {
if (err) {}
doc.remove(callback);
})
}
//Remove comments related to post
PostSchema.pre('remove', function(next) {
this.model('Comment').remove({ post: this._id }, next);
});
但这没有任何作用,因为我不知道如何从我的 CRUD 工厂调用它。不知道这样做对不对。因此,欢迎任何帮助。 :)
当您在 Mongoose 文档上调用 remove()
时,将自动触发 Mongoose 中间件。上面 delete
路由中的代码不会触发中间件,因为它在模型而不是文档上调用 remove()
(关于这个 here 的对话)。
我想你已经拥有了所有的碎片,你只需要移动它们。保留 removePost()
函数和 pre('remove')
中间件的逻辑,并将逻辑合并到删除路由中:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.findById(id, function (err, post) {
if (err) res.send(err);
post.remove(function (err, doc) {
if (err) res.send(err);
res.json({ message: 'Successfully deleted' });
});
})
});
不过,我的方法 "deleting" 不同。我没有实际删除文档,而是向我的模式添加了一个 deleted
字段,并在删除时将该字段更新为 true
。在某些情况下,实际删除文档是有意义的,但要记住这一点。
如果有人想知道究竟是什么解决了我的问题,这里有两种不同的解决方案:
根据 gh0st 的建议,第一个有效的方法是更改我的路由器以进行删除,如下所示:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.remove({_id: req.params.post}, function(err, post) {
if (err) {res.send(err);}
Comment.remove({post: req.params.post}, function(err, post) {
if (err) {res.send(err);}
});
res.json({ message: 'Successfully deleted' });
});
});
holla 建议的第二个也有效的方法是更改我的路由器以进行删除,如下所示:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.findOne({ _id: req.params.post}, function (err, post) {
if (err) {res.send(err);}
post.remove(function (err) {
if (err) {res.send(err);}
res.json({ message: 'Successfully deleted' });
});
});
});
然后向我的架构文件添加一个预挂钩:
// Remove comments related to a post
PostSchema.pre('remove', function(next) {
this.model('Comment').remove({ post: this._id }, next);
});
我正在关注 Thinkster 的 MEAN 堆栈教程 (https://thinkster.io/tutorials/mean-stack),其中并未说明应如何实施删除操作。
我目前有以下架构:
var PostSchema = new mongoose.Schema({
title: {type: String, required: true, maxlength: 140},
link: {type: String, required: true, maxlength: 300},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
var CommentSchema = new mongoose.Schema({
body: {type: String, maxlength: 200, minlength: 1},
post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' }
});
我有一个 Angular 工厂用于 CRUD 操作。我目前使用以下工厂方法删除 posts:
o.deletePost = function(id) {
return $http.delete('/posts/' + id, null, {}
}).success(function(res){
$window.location.href = '/#/home';
return res.data;
});
};
我的删除路由器如下所示:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.remove({_id: req.params.post}, function(err, post) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
不幸的是,当我删除这样的 post 时,任何相关的评论都会留在数据库中的某处。所以,我的问题是 如何在删除 post 的同时删除与 post 相关的所有评论?
我已经尝试 Google 寻找答案,看来我应该以某种方式使用 MongoDB 的中间件。我试过:
// Remove Post
module.exports.removePost = function(id, callback){
Post.findById(id, function (err, doc) {
if (err) {}
doc.remove(callback);
})
}
//Remove comments related to post
PostSchema.pre('remove', function(next) {
this.model('Comment').remove({ post: this._id }, next);
});
但这没有任何作用,因为我不知道如何从我的 CRUD 工厂调用它。不知道这样做对不对。因此,欢迎任何帮助。 :)
当您在 Mongoose 文档上调用 remove()
时,将自动触发 Mongoose 中间件。上面 delete
路由中的代码不会触发中间件,因为它在模型而不是文档上调用 remove()
(关于这个 here 的对话)。
我想你已经拥有了所有的碎片,你只需要移动它们。保留 removePost()
函数和 pre('remove')
中间件的逻辑,并将逻辑合并到删除路由中:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.findById(id, function (err, post) {
if (err) res.send(err);
post.remove(function (err, doc) {
if (err) res.send(err);
res.json({ message: 'Successfully deleted' });
});
})
});
不过,我的方法 "deleting" 不同。我没有实际删除文档,而是向我的模式添加了一个 deleted
字段,并在删除时将该字段更新为 true
。在某些情况下,实际删除文档是有意义的,但要记住这一点。
如果有人想知道究竟是什么解决了我的问题,这里有两种不同的解决方案:
根据 gh0st 的建议,第一个有效的方法是更改我的路由器以进行删除,如下所示:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.remove({_id: req.params.post}, function(err, post) {
if (err) {res.send(err);}
Comment.remove({post: req.params.post}, function(err, post) {
if (err) {res.send(err);}
});
res.json({ message: 'Successfully deleted' });
});
});
holla 建议的第二个也有效的方法是更改我的路由器以进行删除,如下所示:
// DELETE a post
router.delete('/posts/:post', function(req, res, next) {
Post.findOne({ _id: req.params.post}, function (err, post) {
if (err) {res.send(err);}
post.remove(function (err) {
if (err) {res.send(err);}
res.json({ message: 'Successfully deleted' });
});
});
});
然后向我的架构文件添加一个预挂钩:
// Remove comments related to a post
PostSchema.pre('remove', function(next) {
this.model('Comment').remove({ post: this._id }, next);
});