Database
[{"_id":"CF52F598-392B-051E-F74D-869D15191495","user":"self",/**"oldAccount": "old1",*"account": "new1",*/"accountState":"CLOSED"},{"_id":"7a5dda79-69d3-4fc1-af37-15ccf8c69f9f","user":"self","oldAccount":"old1","account":"new1","accountState":"ACTIVE"}]
Query
db.collection.aggregate([/** find all accounts related to user*/{$match:{user:"self"}},/**extract all desired fields* {* $project: {* _id: 1, { ... lots of fields but irrelevant for issue ... }* }* },* Remove closed account with an associated open account*/{$group:{_id:"$oldAccount",relationshipCnt:{$sum:1},tmp:{$push:"$$ROOT"}}},{$unwind:{path:"$tmp",preserveNullAndEmptyArrays:true}},{$replaceRoot:{newRoot:{$mergeObjects:["$tmp",{relationshipCnt:"$relationshipCnt"}]}}},{$match:{$or:[{accountState:{$ne:"CLOSED"}},{accountState:{$regex:"CLOSED",$options:"i"},"relationshipCnt":{$eq:1}}]}}])