Database
[{"glob":[{id:"1",text:"Parent",parent:"#"},{id:"2",text:"child1",parent:"1"},{id:"3",text:"child2",parent:"1"},{id:"7",text:"child7",parent:"1"},{id:"4",text:"grandchild1",parent:"3"},{id:"8",text:"child8",parent:"7"},{id:"9",text:"child9",parent:"7"},]}]
Query
db.collection.aggregate([{$set:{data:{$filter:{input:"$glob",cond:{$eq:["$$this.parent","1"]}}}}},{$project:{_id:0,data:1,childrenExists:{$reduce:{input:"$glob",initialValue:[],in:{$setUnion:["$$value",{$cond:[{$in:["$$this.parent","$data.id"]},["$$this.parent"],[]]}]}}}}},{$unwind:"$data"},{$set:{"data.children":{$cond:[{$in:["$data.id","$childrenExists"]},true,false]},childrenExists:"$$REMOVE"}},{$replaceRoot:{newRoot:"$data"}}])