'MongoDB: remove skill from user document

So I have an issue. I have a MongoDB user document and this document has a property skills, which is an array filled with objects.

What I want to do now is the following. The client sends a request to delete one of these skills. So I send the skill ID and then I want to remove the skill from the user. How could I do this?

What I currently have: every item in the skills property array has a skill property which is an objectID. When this objectID matches the objectId that the client sent, then we want to remove this skill from the user

const removeSkill = async (req, res) => {
  try {
    const { userId, params: { skillId } } = req;

    const user = await User.findByIdAndUpdate({ _id: userId }, {
      $pull: { "skills.skill": skillId }
    }, {
      new: true
    });

    return res.status(200).json({
      message: 'succesfully removed skill',
      user
    });
  } catch (err) {
    return sendErr(res, err);
  }
};

What the user mongodb document looks like

enter image description here

The error I get

:no_entry: Error:
MongoError: Cannot use the part (skill) of (skills.skill) to traverse the element ({skills: [ { _id: ObjectId('5c8729be12e1cc05c04ea182'), name: "javascript", skill: ObjectId('5c8729be
12e1cc05c04ea181'), points: 22 }, { _id: ObjectId('5c8729dc12e1cc05c04ea184'), name: "node.js", skill: ObjectId('5c8729dc12e1cc05c04ea183'), points: 14 }, { _id: ObjectId('5c872a6c12e1c
c05c04ea186'), name: "css", skill: ObjectId('5c872a6c12e


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source