Update certain document field for certain documents

My code has a Meteor collection myCol, each of its documents has a field named “action” with value myAction.

The code also has an object with name:value entries.
doc = {name1:value1, name2:value2,…}

I need to get all the documents with field ‘action’ = ‘myAction’, then from them get the document with field ‘name’ = name1, then update this documents to have a new field “value” and assign to it the value value1.
And do that for all the entries in the doc

I hope I explained it well :slight_smile:

How can I do this please? Thanks

You’ll have to iterate over your doc object:

for (var name in doc) {
  var value = doc[name];
  myCol.update({action: 'myAction', name: name}, {$set: {value: value}});
}

Hopefully that’s what you wanted :slightly_smiling:

1 Like