I'm trying to create a mongodb query to see which invoices are paid or not. I would like to add few things to the outcome like:
- virtualAmount : original amount - (sum of all creditnotes)
- total paid amount : sum of all transactions where delete is false
- paid (true/false) : if virtualAmount - total paid amount is 0
I have created mongo playground:
https://mongoplayground.net/p/0OyK_bOZu9X
- Anyone know if this is possible?
- How to still have the original object when using group?
- Is it also possible to create this result with mongoose?
by $unwind array and $group it.
result
EDIT
or easy way without group