MeteorShower MeteorShower - 3 years ago 276
CoffeeScript Question

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.

Answer Source

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 }});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download