Is there a way to remove sub-document from object without passing the parentId? So I have a document which looks like this:
name: {
type: [String],
trim: true,
required: [true, 'Please add your name']
},
experience: [
{
title: {
type: String,
trim: true,
required: [true, 'Please add an experience title']
}
}
],
Each of this document can have several objects in the experience array.
Adding and updating has been easy for me because I usually just need to pass the Id of the parent document.
Now I would like to delete any object of said experience array by proving only its id...not the parentId.
Hopefull this code can help explain what I'm looking for:
const resume = await Resume.findByIdAndUpdate(
{
// _id: req.params.id, // <=== is the parent id...
experience: { _id: req.params.exp_id }
},
{
$pull: { experience: { _id: req.params.exp_id } }
},
{
new: true,
runValidators: true
}
);
You can see in the code above that I passed the _id of the parent document which if I continue this way, it works great but for my needs I need to only pass the _id of the subdocument.
Is that actually possible?