Database
[{_id:123,topics:[{topicname:"Medical Records",prop:"propdata",messages:[{username:"John Doe",message:"Hello"},{username:"Bill Doe",message:"Hi"},{username:"John Doe",message:"My name is John Doe"}]},{topicname:"Abc",prop:"propdata",messages:[{username:"A",message:"Hello"},{username:"Bill Doe",message:"Hi"},{username:"John Doe",message:"C"}]}]},{_id:124,topics:[{topicname:"d",prop:"propdata",messages:[{username:"John Doe",message:"Hello"},{username:"Bill Doe",message:"Hi"},{username:"John Doe",message:"My name is John Doe"}]},{topicname:"Sports",prop:"propdata",messages:[{username:"John Doe",message:"Hello"},{username:"Bill Doe",message:"Hi"},{username:"John Doe",message:"My name is John Doe"}]}]}]
Query
db.collection.aggregate([{$match:{"topics.messages.username":"John Doe"}},{$unwind:"$topics"},{$project:{"topics.messages":{$map:{input:"$topics.messages",as:"item",in:{$mergeObjects:["$$item",{"username":{$cond:[{$eq:["$$item.username","John Doe"]},"Johnathan Doe","$$item.username"]}}]}}},"topics.topicname":1,"topics.prop":1}},{$group:{_id:"$_id",topics:{$push:"$topics"}}},{$merge:{into:"newCollection"}}])