--- Revision 323031643164 +++ Revision 333538336461 @@ -1,20 +1,23 @@ -// MAP -function(doc) { - if (doc.type && doc.type == 'transaction') { - if (doc.category) emit(doc.category); - if (doc.split) doc.split.map(function(spl) { - if (spl.category) emit(spl.category); - }); - } +// I am creating an accounting app; the transactions are stored using the following doc: + +{ + "_id": "405b791b64424d12e03c12e6e5001535", + "_rev": "1-97d53ba31a6ebb8c0508d0fc8776fac5", + "type": "transaction", + "account": "account_id", + "payee": "Gas Company", + "description": "May Bill", + "date": "2009/05/25", + "status": "cleared", + "category": "Bills / Gas", + "value": -5000 } -// REDUCE -function(keys, values, rereduce) { - return true; -} +// Or they can be split acroll multiple accounts / categories / etc. In this case split over two categories: -// SAMPLE DOC { + "_id": "405b791b64424d12e03c12e6e5002532", + "_rev": "1-da46384d5f9fbfd26acbf8fc9a8e82fe", "type": "transaction", "account": "account_id", "payee": "Credit Card Company", @@ -31,6 +34,23 @@ ] } +// I have made a view to display all the unique categories in use across all transactions. + +// MAP +function (doc) { + if (doc.type && doc.type == 'transaction') { + if (doc.category) emit(doc.category); + if (doc.split) doc.split.map(function (spl) { + if (spl.category) emit(spl.category); + }); + } +} + +// REDUCE +function(keys, values, rereduce) { + return true; +} + // CURRENT OUTPUT OF VIEW (with group=true) { "rows": [ @@ -45,7 +65,7 @@ ] } -// CURRENT OUTPUT OF VIEW (as temp view) +// CURRENT OUTPUT OF VIEW WHEN RUN AS A TEMPORARY VIEW (with group=true) { "rows": [ { @@ -62,81 +82,13 @@ } ] } -// SIMPLIFIED VIEW -function(doc) { - if (doc.type && doc.type == 'transaction') { - emit(null,doc); - } + +// SIMPLE TEST VIEW +function (doc) { + if (doc.split) emit(null, doc.split); } -// OUTPUT -{ - "total_rows": 2, - "offset": 0, - "rows": [ - { - "id": "405b791b64424d12e03c12e6e5001535", - "key": null, - "value": { - "_id": "405b791b64424d12e03c12e6e5001535", - "_rev": "1-97d53ba31a6ebb8c0508d0fc8776fac5", - "type": "transaction", - "account": "account_id", - "payee": "Gas Company", - "description": "May Bill", - "date": "2009/05/25", - "status": "cleared", - "category": "Bills / Gas", - "value": -5000 - } - }, - { - "id": "405b791b64424d12e03c12e6e5002532", - "key": null, - "value": { - "_id": "405b791b64424d12e03c12e6e5002532", - "_rev": "1-da46384d5f9fbfd26acbf8fc9a8e82fe", - "type": "transaction", - "account": "account_id", - "payee": "Credit Card Company", - "description": "May Statement", - "date": "2009/06/02", - "status": "uncleared", - "category": "Debt / Credit Card Payments", - "value": -5000, - "split": [ - ] - } - } - ] -} - -// OUTPUT OF DOC -{ - "_id": "405b791b64424d12e03c12e6e5002532", - "_rev": "1-da46384d5f9fbfd26acbf8fc9a8e82fe", - "type": "transaction", - "account": "account_id", - "payee": "Credit Card Company", - "description": "May Statement", - "date": "2009/06/02", - "status": "uncleared", - "category": "Debt / Credit Card Payments", - "value": -5000, - "split": [ - { - "category": "Dept / Default Charges", - "value": -1000 - } - ] -} - -// TEST VIEW -function(doc) { - if (doc.split) emit(null,doc.split); -} - -// OUTPUT OF TEMP VIEW +OUTPUT OF TEMP VIEW { "total_rows": 1, "offset": 0, @@ -154,7 +106,7 @@ ] } -//OUTPUT OF SAME VIEW AFTER SAVE +OUTPUT OF SAME VIEW AFTER SAVE { "total_rows": 1, "offset": 0, @@ -167,3 +119,11 @@ } ] } + +It seems from the above that couch is for some reason returning the splits field as an empty array. + +Thanks for your time on IRC already. + +Cheers + +John