Database

db={"orders":[{"_id":ObjectId("5cd42f7b16c2654ea9138ece"),"customerId":ObjectId("5c8222109146d119ccc5243f"),"orderAmount":NumberInt(10000),"paidAmount":NumberInt(4000),"installments":[{"dueDate":ISODate("2020-01-01"),"amount":NumberInt(2000)},{"dueDate":ISODate("2020-01-07"),"amount":NumberInt(6000)},{"dueDate":ISODate("2020-01-04"),"amount":NumberInt(2000)}]}]}

Query

db.orders.aggregate([{$unwind:"$installments"},{$sort:{"installments.dueDate":1}},{$group:{_id:"$_id",orders:{$first:"$$ROOT"},installments:{$push:"$installments"},tmp:{$push:"$installments"}}},{$unwind:"$installments"},{$addFields:{"installments.paid":{$cond:[{$gte:[{$reduce:{input:{$slice:["$tmp",{$sum:[{$indexOfArray:["$tmp","$installments"]},1]}]},initialValue:"$orders.paidAmount",in:{$sum:[{$multiply:["$$this.amount",-1]},"$$value"]}}},0]},true,false]}}},{$group:{_id:"$_id",customerId:{$first:"$orders.customerId"},orderAmount:{$first:"$orders.orderAmount"},paidAmount:{$first:"$orders.paidAmount"},installments:{$push:"$installments"}}}])

Result