Database

[{"order_line_item_id":1,"order_id":100,"products":[{"name":"Shoe","hasDefect":"YES"},{"name":"Pant","hasDefect":"NO"},{"name":"Shirt","hasDefect":"NOT_SURE"}]},{"order_line_item_id":2,"order_id":100,"products":[{"name":"Shoe","hasDefect":"YES"},{"name":"Pant","hasDefect":"YES"},{"name":"Shirt","hasDefect":"YES"}]},{"order_line_item_id":3,"order_id":101,"products":[{"name":"Shoe","hasDefect":"YES"},{"name":"Pant","hasDefect":"NO"},{"name":"Shirt","hasDefect":"NOT_SURE"}]}]

Query

db.collection.aggregate([{$unwind:"$products"},{$group:{_id:{order_id:"$order_id",productName:"$products.name"},hasDefects:{$push:"$products.hasDefect"},products:{$push:"$products"}}},{$set:{hasDefect:{$switch:{branches:[{case:{$allElementsTrue:{$map:{input:"$hasDefects",as:"d",in:{$eq:["$$d","YES"]}}}},then:"YES"},{case:{$in:["NO","$hasDefects"]},then:"NO"},{case:{$in:["NOT_SURE","$hasDefects"]},then:"NOT_SURE"}],default:"<DEFAULT>"}}}},{$group:{_id:"$_id.order_id",products:{$push:{name:"$_id.productName",hasDefect:"$hasDefect"}}}},{$merge:{into:"order",whenMatched:[{$addFields:{products:{$let:{vars:{newProducts:"$$new.products"},in:{$concatArrays:[{$map:{input:"$products",as:"oldProduct",in:{$cond:{if:{$in:["$$oldProduct.name","$$newProducts.name"]},then:{$first:{$filter:{input:"$$newProducts",cond:{$eq:["$$this.name","$$oldProduct.name"]}}}},else:"$$oldProduct"}}}},{$filter:{input:"$$newProducts",cond:{$not:{$in:["$$this.name","$products.name"]}}}}]}}}}}],whenNotMatched:"insert"}}])

Result