I am fairly new to MongoDB, having background in SQL databases. I have 2 collections : an invoice collections and payments collection that holds payment documents for each invoice. I need to retrieve all the invoices and also have a field named totalPayedAmount that sums the amount of the total payments for that invoice. I was thinking of doing this by adding a field in the invoice collection named totalPayedAmount and updating it each time I add a new payment for that invoice. I think in MongoDB this can be made easier with aggregation functions or with mapReduce but I am not really sure how
This is the schema:
invoice collection:
{
_id
invoiceNumber
...
}
payment collection:
{
_id
invoiceId
amount
}