Database
[{_id:ObjectId("65bc98f235375d1d97621e06"),name:"Aspirin",type:"Pain Relief",forms:[{formType:"TabletsData",strength:"100mg",dosageInstructions:[{time:"Morning",instructions:[{step:1,text:"Take 1 tablet with breakfast.",_id:ObjectId("65bc98f235375d1d97621e09")},{step:2,text:"Drink a full glass of water.",_id:ObjectId("65bc98f235375d1d97621e0a")}],_id:ObjectId("65bc98f235375d1d97621e08")},{time:"Evening",instructions:[{step:1,text:"Take 1 tablet with dinner.",_id:ObjectId("65bc98f235375d1d97621e0c")},{step:2,text:"Avoid taking on an empty stomach.",_id:ObjectId("65bc98f235375d1d97621e0d")}],_id:ObjectId("65bc98f235375d1d97621e0b")}],_id:ObjectId("65bc98f235375d1d97621e07")},{formType:"Capsule",strength:"200mg",dosageInstructions:[{time:"Afternoon",instructions:[{step:1,text:"Take 1 capsule in the afternoon.",_id:ObjectId("65bc98f235375d1d97621e10")},{step:2,text:"Take with or after food.",_id:ObjectId("65bc98f235375d1d97621e11")}],_id:ObjectId("65bc98f235375d1d97621e0f")},{time:"Night",instructions:[{step:1,text:"Take 1 capsule before bedtime.",_id:ObjectId("65bc98f235375d1d97621e13")},{step:2,text:"Avoid taking with dairy products.",_id:ObjectId("65bc98f235375d1d97621e14")}],_id:ObjectId("65bc98f235375d1d97621e12")}],_id:ObjectId("65bc98f235375d1d97621e0e")}],ingredients:["Acetylsalicylic Acid","Inactive Ingredient 1","Inactive Ingredient 2"],__v:0}]
Query
db.collection.update({},[{"$addFields":{"forms":{"$map":{"input":"$forms","as":"f","in":{$mergeObjects:["$$f",{"dosageInstructions":{"$map":{"input":"$$f.dosageInstructions","as":"d","in":{$mergeObjects:["$$d",{"instructions":{"$map":{"input":"$$d.instructions","as":"i","in":{$cond:{if:{$eq:["$$i.step",1]},then:{$mergeObjects:["$$i",{"text":"New Value"}]},else:"$$i"}}}}}]}}}}]}}}}}],{multi:true})