Search code examples
meteorcollectionscoffeescript

Update value for all objects in collection Meteor


I have a meteor program that I am working on and I have a collection for Teachers.

I can update their pay with the following snippet

MyTeacherCollection.update {_id: teacherId}, $set:
  payRate: newPayrate

But that is only one teacher at a time, I was wondering how if there is an easy way to apply a 5% raise to all the teacher 's salary in my collection.


Solution

  • Since each teacher's salary differs you need to iterate over the collection:

    MyTeacherCollection.find().forEach(t => {
      const oldRate = t.payRate;
      MyTeacherCollection.update(t._id,{ $set: { payRate: oldRate*1.05 }});
    });