Database
[{"display_name":"test name 1","contact_info":{"registrar_phone":null,/** remove null attribute from response*/"registrar_email":null,"addresses":[{"addr2":null,/** remove null attribute from nested array*/"zipcode":"7102"}]},"score":1.812557339668274},{"display_name":"test name 2","contact_info":{"registrar_phone":null,/** remove null attribute from response*/"registrar_email":null,"addresses":[{"addr2":null,/** remove null attribute from nested array*/"zipcode":"7103"}]},"score":1.812557339668275},{"display_name":"test name 2","contact_info":{"registrar_phone":"566",/** remove null attribute from response*/"registrar_email":null,"addresses":[{"addr2":"b",/** remove null attribute from nested array*/"zipcode":"7103"}]},"score":1.812557339668275}]
Query
db.collection.aggregate([{$set:{"contact_info.registrar_phone":{"$ifNull":["$contact_info.registrar_phone","$$REMOVE"]},"contact_info.registrar_email":{"$ifNull":["$contact_info.registrar_email","$$REMOVE"]},"contact_info.addresses":{$filter:{input:"$contact_info.addresses",as:"item",cond:{"$ifNull":["$$item.addr2","$$REMOVE"]}}}}},{$set:{"contact_info.addresses":{$cond:[{$eq:[{$size:"$contact_info.addresses"},0]},"$$REMOVE","$contact_info.addresses"]}}}])