Database
[{_id:11,marksObtained:[{physics:10},{chemistry:12},{maths:11},{biology:9},{social:9},{english:8}],name:"Someone"},{_id:12,marksObtained:[{physics:1},{chemistry:1},{maths:1},{biology:1},{social:1}],name:"Sometwo"}]
Query
db.collection.update({_id:11},[{$set:{marksObtained:{$function:{body:"function(marks) { const updateFields = { chemistry: 13, zoology: 12, french: 11 }; for (const updateFieldName in updateFields) { let updated = false;for (const markObtained of marks) { for (const markObtainedKey in markObtained) { if (markObtainedKey === updateFieldName) { markObtained[markObtainedKey] = updateFields[updateFieldName]; updated = true; } } } if (!updated) { const newTopic = {}; newTopic[updateFieldName] = updateFields[updateFieldName]; marks.push(newTopic); } } return marks }",args:["$marksObtained"],lang:"js"}}}}])