'How to remove specific field from table in node js mongodb?
comment: 'hi'
replies: array
likes: array
I have to remove comment field and every time I delete it deletes the whole collections.
This is the code
deleteComment: async (root, { commentID, comment}) => {
try{
const deleteComment = await CommentModel.findOne(
{_id:commentID}
);
if(deleteComment) {
const remove = await CommentModel.deleteOne(comment)
return(remove)
}
} catch(err) {
console.log(err)
}
},
Solution 1:[1]
Just delete by ID.
CommentModel.deleteOne({_id: new mongodb.ObjectID(commentID)});
I'm assuming you are using the mongodb npm package.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mstephen19 |
