I have a collection with a key called fields, which is an array of JSON objects. Those objects can have options which is another array of JSON objects. I’m trying to update one of the options by optionId. I tried this but it doesn't work.
Projects.update({
'fields.options._id': optionId
}, {
$set: {
`fields.$.options.$.title`: title
}
}
This does find the correct Project document, but doesn't update it.
You can use the
$operator for single level arrays only. Use ofarray1.$.array2.$.keyis not supported.However, if you are aware of the exact index of the element to be updated within the array, you can update like so:
This is one way to update:
Source