Search code examples
mapreducecouchdbcouchdb-2.0fauxton

Get total invoices per customer


I have a database where I have docs like customers (customerId, firstName, lastName, phoneNo, eMail) and invoice (invoiceId, invoiceDate, customerId).

I want to get the sum of invoices per customer in a view. I'm currently using Fauxton 2.0.

My view is currently:

function (doc) {
    if (doc.customerId && doc.invoiceId) {
        emit(doc.customerId, doc.invoiceId);
    }
}

But I want to add some sort of reduce function which can give me the total sum of invoices.

thanks.


Solution

  • Well, I solved the problem, it was just an issue of changing the reduce function _sum to _count, to get the total amount of invoices. So when requesting the key(customerId), I get the count.