I am trying to update a field inside array of objects, where field in nested array is equal to a value.
My goal here is to set the picture
field a new url, where value
field in valueList
is oldRed
Product schema:
{
variations: [{
id: 1,
picture: 'https://example.picture.com',
valueList: [{
name: 'color',
value: 'oldRed'
}, {
name: 'size',
value: 'M'
}]
}, {
id: 2,
picture: 'https://example.picture.com',
valueList: [{
name: 'color',
value: 'black'
}, {
name: 'size',
value: 'M'
}]
}]
}
The closest I get is thanks to this answer, where I update all my nested array fields that contains :'oldRed' . But my final goal is to update other field picture
, based on nested array field.
db.Product.findOneAndUpdate({
_id: '123'
}, {
$set: {
'variations.$[].valueList.$[nameField].value': 'newRed'
}
}, {
arrayFilters: [{
'nameField.value': 'oldRed'
}],
new: true
}
});