When it comes to your specific needs -
If you're looking to calculate a value for each item, purely for display purposes (such as fields for the template):
Items.find({ /* selector */ }, {
transform: function(item){
item.newField = true;
return item;
}
});
However, if your goal is to update every document with unique values in MongoDB using Meteor's APIs:
var items = Items.find({ /* selector */});
items.forEach(function(item){
var someValue = computeSomeValue(item);
Items.update({
_id: item._id
}, {
$set: {
newField: someValue
}
});
});
Alternatively, if you simply want to update all matched items with the SAME value:
Items.update({ /* selector */}, {
$set: {
newField: true
},
},
{
multi: true
}
);
If you are performing this task on the client-side in Meteor, the success of the last two options will also hinge on utilizing the insecure
package or establishing appropriate allow
or deny
rules on the Items
collection.