Database

db={"Forests":[{forest_ranger:"bob",some_other_data:{"data":"Dummy data"},forests:[{forest_id:"forestA",trees:[{tree_id:"treeA",branches:[{branch_id:"branchA"},{branch_id:"branchA1"},{branch_id:"branchA2"}]},{tree_id:"treeB",branches:[{branch_id:"branchB"}]}]},{forest_id:"forestB",trees:[{tree_id:"treeC",branches:[{branch_id:"branchC"}]},{tree_id:"treeD",branches:[{branch_id:"branchD"}]}]},{forest_id:"forestC",trees:[{tree_id:"treeE",branches:[{branch_id:"branchE"}]}]}]}],"Leaves":[{leaf_id:"leafA",branch_id:"branchA"},{leaf_id:"leafA1",branch_id:"branchA1"},{leaf_id:"leafB",branch_id:"branchB"},{leaf_id:"leafC",branch_id:"branchC"}]}

Query

db.Forests.aggregate([{$unwind:"$forests"},{$unwind:"$forests.trees"},{$unwind:"$forests.trees.branches"},{$lookup:{from:"Leaves",localField:"forests.trees.branches.branch_id",foreignField:"branch_id",as:"forests.trees.branches.leaves"}},{$addFields:{"forests.trees.branches.leaf_count":{$size:"$forests.trees.branches.leaves"}}},{$project:{"forests.trees.branches.leaves":0}},{$group:{_id:{forest_id:"$forests.forest_id",tree_id:"$forests.trees.tree_id"},forest_ranger:{$first:"$forest_ranger"},some_other_data:{$first:"$some_other_data"},leaf_count:{$sum:"$forests.trees.branches.leaf_count"},branches:{$push:"$forests.trees.branches"}}},{$sort:{"_id.tree_id":1}},{$group:{_id:"$_id.forest_id",forest_ranger:{$first:"$forest_ranger"},some_other_data:{$first:"$some_other_data"},leaf_count:{$sum:"$leaf_count"},trees:{$push:{leaf_count:{$sum:"$leaf_count"},tree_id:"$_id.tree_id",branches:"$branches"}}}},{$sort:{_id:1}},{$group:{_id:null,forest_ranger:{$first:"$forest_ranger"},some_other_data:{$first:"$some_other_data"},leaf_count:{$sum:"$leaf_count"},forests:{$push:{leaf_count:{$sum:"$leaf_count"},forest_id:"$_id",trees:"$trees"}}}},{$project:{_id:0}}])

Result