Database

[{"yearMonth":201908,amount:100},{"yearMonth":201909,amount:100},{"yearMonth":201910,amount:200},{"yearMonth":201911,amount:100},{"yearMonth":201912,amount:200},{"yearMonth":202001,amount:300},{"yearMonth":202002,amount:200}]

Query

db.collection.aggregate([{$group:{_id:"",data:{$push:"$$ROOT"}}},{$project:{data:{$let:{vars:{data:{$reduce:{input:"$data",initialValue:{amount:0,docs:[]},in:{docs:{$concatArrays:["$$value.docs",[{_id:"$$this._id",yearMonth:"$$this.yearMonth",amount:"$$this.amount",balance:{$add:["$$value.amount","$$this.amount"],},},],],},amount:{$add:["$$value.amount","$$this.amount"]},},},},},in:"$$data.docs",},},},},{$unwind:"$data",},{$replaceRoot:{newRoot:"$data",},}])

Result