Database
[{id:1,text:"Parent",xpath:"/Parent",parent:"#",children:true},{id:2,text:"Child1",xpath:"/Parent/Child1",parent:1,children:false},{id:3,text:"Child2",xpath:"/Parent/Child2",parent:1,children:true},{id:4,text:"Child3",xpath:"/Parent/Child3",parent:1,children:false},{id:5,text:"Grandchild1",xpath:"/Parent/Child2/Grandchild1",parent:3,children:false},{id:6,text:"Grandchild2",xpath:"/Parent/Child2/Grandchild2",parent:3,children:false},{id:7,text:"Grandchild3",xpath:"/Parent/Child2/Grandchild3",parent:3,children:false}]
Query
db.collection.aggregate([{"$match":{"id":1}},{"$graphLookup":{"from":"collection","startWith":"$id","connectFromField":"id","connectToField":"parent","as":"descendents","depthField":"generation"}},{"$unwind":"$descendents"},{"$sort":{"descendents.id":1}},{"$group":{"_id":"$descendents.generation","theChildren":{"$push":"$descendents"}}},{"$project":{"_id":0,"theChildren":1,"generation":{"$toInt":"$_id"},"parent":{"$first":"$theChildren.parent"}}}])