Using Express and MongoDB, how can I update a document, then get that updated document?
The following code will find the profile document with the given user.id, then remove the experience with the given exp_id from the experience array. However, it returns the original profile (including the removed experience), not the updated one.
let profile = await Profile.findOneAndUpdate(
{ user: req.user.id },
{ $pull: { experience: { _id: req.params.exp_id } } },
{ returnNewDocument: true }
)
console.log(profile); // logs the original profile, still incl the removed experience
What do I need to change for it to return the updated profile?
Docs: https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/