I have a Post model and there is array of comments inside him (objectsId that refer to the Comment model). When I am deleting a post i want to delete his comments too I was trying to use forEach and its worked :
post.comments.forEach(async (comment) => {
await comment.remove({ session: postDeletingSession });
});
I am sure that there is a better way to do that, can somone help me to find one?
CodePudding user response:
If post.comments
is an array of ObjectIDs, you can use Model.deleteMany()
as shown below:
await comment.deleteMany({ _id: { $in: post.comments } })