I was reading the docs but could not find a way to do this "double" updates.
First the collection:
meteor:PRIMARY> db.radioOptions.find({}).pretty();
{
"_id" : "n8DsDzsPL8JTRFEQ8",
"name" : "optionType",
"value" : [
{
"name" : "1stOption",
"caption" : "1st Option",
"checked" : true <-------- run code added this property
},
{
"name" : "2ndOption",
"caption" : "2nd Option",
"checked" : true <-------- run code added this property
}
]
}
And here is the code that run which added the marked property. credit
RadioOptions.update(
{name: obj.name, 'value.name': obj.value},
{$set: {'value.$.checked': true}}
);
But I also want to either set the other embedded documents to false or remove their property checked:true
so that there will only be one embedded document with its checked field is set to either true or checked.
My failed attempt: bruteforce
RadioOptions.update(
{name: obj.name, 'value.name': obj.value},
{$set: {'value.$.checked': true}, $set: {$ne: {'value.$.checked': false}}}
);
How can this get done? Can I do this in one run of the update method ?
edit After reading the link suggested as a duplicate.
My updated code below is failing in that the update lines are updating the first doc with checked:false
and the second with checked:true
no mater what the condition evaluates to, which has been verified to evaluate correctly.
var doc = RadioOptions.findOne({name: obj.name});
if (typeof doc != 'undefined') {
doc.value.forEach(function (embdoc) {
// how to update so that to use $unet the "checked" property of all embedded documents in the value array?
if (embdoc.name == obj.value) {
RadioOptions.update(
{name: obj.name, 'value.name': obj.value},
{$set: {'value.$.checked': true}}
);
}
})
}