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:[]},{id:6,tags:["b"],childs:[5]},{id:7,tags:[],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"}}}]}},{$unwind:"$ids"},{$group:{_id:null,ids:{$push:"$ids.id"}}}])