For now in case I want to remove a subdocument I do this:
Post.findById(post_id).exec(function(err, post) {
post.comments.remove({'_id': comment_id});
post.save(function(err) {
res.end("Hooray!");
});
});
Which seem to be non-optimal to me, because each time I remove a comment the whole post will be fetched from DB and it has a lot of stuff attached to it. So, is it possible to modify subdocuments without fetching a parent document?
According to documentation if you want to update document without fetching it, you have to perform update request manually. In my case it will result in this code:
Post.update({'_id': post_id}, {$pull: {'comments': {'_id': comment_id}}}).exec(function(err){
console.log('Hooray!');
});
It is a good workable solution, but performing direct request means that validations wouldn't work as well as versioning.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.