0

I'm trying to remove an Schema object from an array in a document using SimpleSchema and Mongo.

TrainingSchema = new SimpleSchema({
  ....
  // irrelevant fields
  ....
exercises: {
    type: Array,
    optional: true,
    autoform: {
        type: "hidden"
    }
},
'exercises.$': {
    type: ExerciseSchema
}
});

An inserted ExerciseSchema Object into the trainingSchema looks like this. sample Array

to insert the Object I'm using this. It works:

addToTraining(trainingId, exercise) {
  Trainings.update(trainingId, {$addToSet: {exercises: exercise}});
}

But to remove the whole object from the array I tried this and some others.

removeExerciseFromTraining(trainingId, exercise) {
  Trainings.update(trainingId, {$pull: {exercises: {_id: exercise._id}}});
}

How can I do this?

Rionidas
  • 1
  • 2
  • Your approach looks ok to me. What's in your `trainingId` variable? – dnickless Oct 16 '17 at 19:21
  • It's the _id from the Training Object. In the image its `AAT5xhqTgjmW4QBs8`. If I change the `'exercises.$': type: ExerciseSchema` to `Object` afterwards I can remove the added Exercise Objects. But with `type: Object` I can't add Exercises. :/ – Rionidas Oct 16 '17 at 19:36
  • 1
    Possible duplicate of [meteor $pull remove from array](https://stackoverflow.com/questions/19362539/meteor-pull-remove-from-array) – dnickless Oct 16 '17 at 19:58

0 Answers0