1

I want to update a collection called SMUProfiles, through a method called classroom.delete. I want to pull out the classroom_id from 2 places inside SMUProfiles i.e. one inside classrooms.owner which has an array of codes, and the other inside array classrooms.students.

I have successfully one the $set part, and now trying to add the $pull, but $pull doesn't seem to work.

Can we do the $set and $pull in such way?

/* Method for deleting Classroom */
'classroom.delete'(classroom_id) {
  if (!this.userId) {
    throw new Meteor.Error('not-authorised');
  }
  Classrooms.remove(classroom_id)
  let classids = Classrooms.find({ owner: this.userId }).fetch().map(function(classrooms){
    return classrooms._id })
  //console.log(classids);
  SMUProfiles.update({
      owner: this.userId,
    }, {
      $set: {
        'classrooms.owner': classids
      },
      $pull: {
        'classrooms.students': classroom_id
      }
    }
  )
}
Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
Hafiz Hanif
  • 361
  • 1
  • 3
  • 20
  • What exactly defines `does not work`? Any errors or unexpected behavior experienced? If so, what behavior did you expect? – Jankapunkt Jan 16 '19 at 05:48
  • @Jankapunkt the `$pull` did not pull out the `classroom_id` from a list of array inside `classrooms.students`. – Hafiz Hanif Jan 16 '19 at 06:34

1 Answers1

1

You're trying to $set and $pull on the same field in the same update - the two operations conflict; so no, you can't use these operators in this way.

You could easily split this into two:

SMUProfiles.update(
  { owner: this.userId },
  { $set: { 'classrooms.owner': classids },
);
SMUProfiles.update(
  { owner: this.userId },
  { $pull: { 'classrooms.students': classroom_id },
);

See e.g. this answer

rubie
  • 1,906
  • 2
  • 20
  • 26