Database
[{"PartnerName":"Company A - Both Complete Equal","MarketExpertese1":[{"Sector":{"Code":1,"Label":"Sector 1"}},{"Sector":{"Code":2,"Label":"Sector 2"}}],"MarketExpertese2":[{"Sector":{"Code":1,"TotalDays":1}},{"Sector":{"Code":2,"TotalDays":1}}]},{"PartnerName":"Company B - 1 has more","MarketExpertese1":[{"Sector":{"Code":1,"Label":"Sector 1"}},{"Sector":{"Code":2,"Label":"Sector 2"}},{"Sector":{"Code":3,"Label":"Sector 3"}}],"MarketExpertese2":[{"Sector":{"Code":1,"TotalDays":1}},{"Sector":{"Code":2,"TotalDays":1}}]},{"PartnerName":"Company C - 2 has more","MarketExpertese1":[{"Sector":{"Code":1,"Label":"Sector 1"}},{"Sector":{"Code":2,"Label":"Sector 2"}}],"MarketExpertese2":[{"Sector":{"Code":1,"TotalDays":1}},{"Sector":{"Code":2,"TotalDays":1}},{"Sector":{"Code":3,"TotalDays":1}}]},{"PartnerName":"Company D - 1 has - 2 Null","MarketExpertese1":[{"Sector":{"Code":1,"Label":"Sector 1"}}],"MarketExpertese2":null},{"PartnerName":"Company E - 1 null - 2 has","MarketExpertese1":null,"MarketExpertese2":[{"Sector":{"Code":1,"TotalDays":1}}]},{"PartnerName":"Company F - Both Empty","MarketExpertese1":[],"MarketExpertese2":[]},{"PartnerName":"Company G - Both Null","MarketExpertese1":null,"MarketExpertese2":null}]
Query
db.collection.aggregate([{$addFields:{MarketExpertese1:{$ifNull:["$MarketExpertese1",[]]},MarketExpertese2:{$ifNull:["$MarketExpertese2",[]]}}},{$addFields:{MarketExpertese3:{$map:{input:"$MarketExpertese2",as:"m",in:{Sector:{$mergeObjects:["$$m.Sector",{$reduce:{input:"$MarketExpertese1",initialValue:{},in:{$cond:[{$eq:["$$m.Sector.Code","$$this.Sector.Code"]},"$$this.Sector","$$value"]}}}]}}}}}},{$addFields:{MarketExpertese3:{$concatArrays:["$MarketExpertese3",{$filter:{input:{$map:{input:"$MarketExpertese1",as:"m",in:{$cond:[{$eq:[{$filter:{input:"$MarketExpertese2",cond:{$eq:["$$this.Sector.Code","$$m.Sector.Code"]}}},[]]},"$$m","$$REMOVE"]}}},cond:{$ne:["$$this",null]}}}]}}}])