1

I am trying to remove multiple objects that are in an array in mongoose. My Workout model look like this:

{
  _id: 5e04068491a2d433007026cd,
  exercises: [
     { _id: 5e0401b9dda7ea28a70e99ed, reps: '1', sets: '3' },
     { _id: 5e0401cadda7ea28a70e99ee, reps: '1', sets: '3' },
     { _id: 5e0401dbdda7ea28a70e99ef, reps: '1', sets: '3' }
   ]
}

I have an array of id's, named deletedExercises, these are the ids of the objects that I want removed from the exercise list. I am trying to loop through deletedExercise and remove any exercises that match the id of the deletedExercise item.

router.put("/:workoutId", (req, res)=>{
  deletedOnes = req.body.exerciseId
    deletedExercises = []
    if(typeof deletedOnes === 'object'){
        deletedOnes.forEach(item => {
            deletedExercises.push(item)
        })
    } else {
        deletedExercises.push(deletedOnes)
    }
    deletedExercises.forEach(item => {
      Workout.findByIdAndUpdate( req.params.workoutId, 
        { $pull: { exercises: { _id: item} } } )
});

0

2 Answers 2

1

You can simply delete exercises using the $in operator inside $pull like this:

router.put("/:workoutId", (req, res) => {
  console.log(req.body.exerciseId); //[ '5e05c5306e964f0a549469b8', '5e05c5306e964f0a549469b6' ]

  Workout.findByIdAndUpdate(
    req.params.workoutId,
    {
      $pull: {
        exercises: {
          _id: {$in: req.body.exerciseId}
        }
      }
    },
    { new: true }
  )
    .then(doc => {
      res.send(doc);
    })
    .catch(err => {
      console.log(err);
      res.status(500).send("Error");
    });
});

Let's say we have this workout with 3 exercises:

{
    "_id": "5e05c5306e964f0a549469b5",
    "exercises": [
        {
            "_id": "5e05c5306e964f0a549469b8",
            "reps": 8,
            "sets": 4
        },
        {
            "_id": "5e05c5306e964f0a549469b7",
            "reps": 10,
            "sets": 3
        },
        {
            "_id": "5e05c5306e964f0a549469b6",
            "reps": 12,
            "sets": 2
        }
    ],
}

If we want to remove the exercises 5e05c5306e964f0a549469b8 and 5e05c5306e964f0a549469b6 for this 5e05c5306e964f0a549469b5 workout, we can send a PUT request with this body: (url must end something like this http://.../5e05c5306e964f0a549469b5)

{
    "exerciseId": [
        "5e05c5306e964f0a549469b8",
        "5e05c5306e964f0a549469b6"
    ]
}

The response will be:

{
    "_id": "5e05c5306e964f0a549469b5",
    "exercises": [
        {
            "_id": "5e05c5306e964f0a549469b7",
            "reps": 10,
            "sets": 3
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow that was a lot simpler than I was making it. thanks for the clear and well structured answer!!
0

Hard to tell considering you're not saying what error you are getting, but my guess from looking at it is that you are comparing an ObjectId with a String, try to replace this line:

{ $pull: { exercises: { _id: item} } } )

with this:

{ $pull: { exercises: { _id: new ObjectId(item)} } } )

** EDIT **

you probably need to also convert the main ID you are searching for to an ObjectId:

Workout.findByIdAndUpdate( new ObjectId(req.params.workoutId), 
    { $pull: { exercises: { _id: new ObjectId(item)} } } )

2 Comments

Alright I'll try that, and I'm not getting any error, the items just dont get deleted, nothing happends
Your solution doesn't do anything either, the types of the id's are now both objects which is good but still nothing is being removed from the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.