Database
[{id:1,tags:["a","b"],childs:[2,3]},{id:2,tags:["a"],childs:[]},{id:3,tags:[],childs:[]},{id:4,tags:["c"],childs:[5]},{id:5,tags:["c"],childs:[]},]
Query
db.collection.aggregate([{$match:{tags:"b",childs:{$ne:[]}}},{$lookup:{from:"collection",let:{childs:"$childs"},as:"ids",pipeline:[{$match:{$expr:{$in:["$id","$$childs"]},tags:{$ne:"b"}}}]}},{$project:{id:1,ids:{$reduce:{input:"$ids",initialValue:[],in:{$concatArrays:["$$value",["$$this.id"]]}}}}}])