| a | b | |
|---|
| 0 | | - | // MAP |
|---|
| 0 | | - | function(doc) { |
|---|
| 0 | | - | if (doc.type && doc.type == 'transaction') { |
|---|
| 0 | | - | if (doc.category) emit(doc.category); |
|---|
| 0 | | - | if (doc.split) doc.split.map(function(spl) { |
|---|
| 0 | | - | if (spl.category) emit(spl.category); |
|---|
| 0 | | - | }); |
|---|
| 0 | | - | } |
|---|
| 0 | + | // I am creating an accounting app; the transactions are stored using the following doc: |
|---|
| 0 | + | |
|---|
| 0 | + | { |
|---|
| 0 | + | "_id": "405b791b64424d12e03c12e6e5001535", |
|---|
| 0 | + | "_rev": "1-97d53ba31a6ebb8c0508d0fc8776fac5", |
|---|
| 0 | + | "type": "transaction", |
|---|
| 0 | + | "account": "account_id", |
|---|
| 0 | + | "payee": "Gas Company", |
|---|
| 0 | + | "description": "May Bill", |
|---|
| 0 | + | "date": "2009/05/25", |
|---|
| 0 | + | "status": "cleared", |
|---|
| 0 | + | "category": "Bills / Gas", |
|---|
| 0 | + | "value": -5000 |
|---|
| ... | |
|---|
| 8 | 8 | | } |
|---|
| 9 | 9 | | |
|---|
| ... | |
|---|
| 10 | | - | // REDUCE |
|---|
| 10 | | - | function(keys, values, rereduce) { |
|---|
| 10 | | - | return true; |
|---|
| 10 | | - | } |
|---|
| 10 | + | // Or they can be split acroll multiple accounts / categories / etc. In this case split over two categories: |
|---|
| ... | |
|---|
| 14 | 14 | | |
|---|
| ... | |
|---|
| 15 | | - | // SAMPLE DOC |
|---|
| ... | |
|---|
| 16 | 16 | | { |
|---|
| ... | |
|---|
| 17 | + | "_id": "405b791b64424d12e03c12e6e5002532", |
|---|
| 17 | + | "_rev": "1-da46384d5f9fbfd26acbf8fc9a8e82fe", |
|---|
| ... | |
|---|
| 17 | 17 | | "type": "transaction", |
|---|
| 18 | 18 | | "account": "account_id", |
|---|
| 19 | 19 | | "payee": "Credit Card Company", |
|---|
| ... | |
|---|
| 30 | 30 | | ] |
|---|
| 31 | 31 | | } |
|---|
| 32 | 32 | | |
|---|
| ... | |
|---|
| 33 | + | // I have made a view to display all the unique categories in use across all transactions. |
|---|
| 33 | + | |
|---|
| 33 | + | // MAP |
|---|
| 33 | + | function (doc) { |
|---|
| 33 | + | if (doc.type && doc.type == 'transaction') { |
|---|
| 33 | + | if (doc.category) emit(doc.category); |
|---|
| 33 | + | if (doc.split) doc.split.map(function (spl) { |
|---|
| 33 | + | if (spl.category) emit(spl.category); |
|---|
| 33 | + | }); |
|---|
| 33 | + | } |
|---|
| 33 | + | } |
|---|
| 33 | + | |
|---|
| 33 | + | // REDUCE |
|---|
| 33 | + | function(keys, values, rereduce) { |
|---|
| 33 | + | return true; |
|---|
| 33 | + | } |
|---|
| 33 | + | |
|---|
| ... | |
|---|
| 33 | 33 | | // CURRENT OUTPUT OF VIEW (with group=true) |
|---|
| 34 | 34 | | { |
|---|
| 35 | 35 | | "rows": [ |
|---|
| ... | |
|---|
| 44 | 44 | | ] |
|---|
| 45 | 45 | | } |
|---|
| 46 | 46 | | |
|---|
| ... | |
|---|
| 47 | | - | // CURRENT OUTPUT OF VIEW (as temp view) |
|---|
| 47 | + | // CURRENT OUTPUT OF VIEW WHEN RUN AS A TEMPORARY VIEW (with group=true) |
|---|
| ... | |
|---|
| 48 | 48 | | { |
|---|
| 49 | 49 | | "rows": [ |
|---|
| 50 | 50 | | { |
|---|
| ... | |
|---|
| 61 | 61 | | } |
|---|
| 62 | 62 | | ] |
|---|
| 63 | 63 | | } |
|---|
| ... | |
|---|
| 64 | | - | // SIMPLIFIED VIEW |
|---|
| 64 | | - | function(doc) { |
|---|
| 64 | | - | if (doc.type && doc.type == 'transaction') { |
|---|
| 64 | | - | emit(null,doc); |
|---|
| 64 | | - | } |
|---|
| 64 | + | |
|---|
| 64 | + | // SIMPLE TEST VIEW |
|---|
| 64 | + | function (doc) { |
|---|
| 64 | + | if (doc.split) emit(null, doc.split); |
|---|
| ... | |
|---|
| 69 | 69 | | } |
|---|
| 70 | 70 | | |
|---|
| ... | |
|---|
| 71 | | - | // OUTPUT |
|---|
| 71 | | - | { |
|---|
| 71 | | - | "total_rows": 2, |
|---|
| 71 | | - | "offset": 0, |
|---|
| 71 | | - | "rows": [ |
|---|
| 71 | | - | { |
|---|
| 71 | | - | "id": "405b791b64424d12e03c12e6e5001535", |
|---|
| 71 | | - | "key": null, |
|---|
| 71 | | - | "value": { |
|---|
| 71 | | - | "_id": "405b791b64424d12e03c12e6e5001535", |
|---|
| 71 | | - | "_rev": "1-97d53ba31a6ebb8c0508d0fc8776fac5", |
|---|
| 71 | | - | "type": "transaction", |
|---|
| 71 | | - | "account": "account_id", |
|---|
| 71 | | - | "payee": "Gas Company", |
|---|
| 71 | | - | "description": "May Bill", |
|---|
| 71 | | - | "date": "2009/05/25", |
|---|
| 71 | | - | "status": "cleared", |
|---|
| 71 | | - | "category": "Bills / Gas", |
|---|
| 71 | | - | "value": -5000 |
|---|
| 71 | | - | } |
|---|
| 71 | | - | }, |
|---|
| 71 | | - | { |
|---|
| 71 | | - | "id": "405b791b64424d12e03c12e6e5002532", |
|---|
| 71 | | - | "key": null, |
|---|
| 71 | | - | "value": { |
|---|
| 71 | | - | "_id": "405b791b64424d12e03c12e6e5002532", |
|---|
| 71 | | - | "_rev": "1-da46384d5f9fbfd26acbf8fc9a8e82fe", |
|---|
| 71 | | - | "type": "transaction", |
|---|
| 71 | | - | "account": "account_id", |
|---|
| 71 | | - | "payee": "Credit Card Company", |
|---|
| 71 | | - | "description": "May Statement", |
|---|
| 71 | | - | "date": "2009/06/02", |
|---|
| 71 | | - | "status": "uncleared", |
|---|
| 71 | | - | "category": "Debt / Credit Card Payments", |
|---|
| 71 | | - | "value": -5000, |
|---|
| 71 | | - | "split": [ |
|---|
| 71 | | - | ] |
|---|
| 71 | | - | } |
|---|
| 71 | | - | } |
|---|
| 71 | | - | ] |
|---|
| 71 | | - | } |
|---|
| 71 | | - | |
|---|
| 71 | | - | // OUTPUT OF DOC |
|---|
| 71 | | - | { |
|---|
| 71 | | - | "_id": "405b791b64424d12e03c12e6e5002532", |
|---|
| 71 | | - | "_rev": "1-da46384d5f9fbfd26acbf8fc9a8e82fe", |
|---|
| 71 | | - | "type": "transaction", |
|---|
| 71 | | - | "account": "account_id", |
|---|
| 71 | | - | "payee": "Credit Card Company", |
|---|
| 71 | | - | "description": "May Statement", |
|---|
| 71 | | - | "date": "2009/06/02", |
|---|
| 71 | | - | "status": "uncleared", |
|---|
| 71 | | - | "category": "Debt / Credit Card Payments", |
|---|
| 71 | | - | "value": -5000, |
|---|
| 71 | | - | "split": [ |
|---|
| 71 | | - | { |
|---|
| 71 | | - | "category": "Dept / Default Charges", |
|---|
| 71 | | - | "value": -1000 |
|---|
| 71 | | - | } |
|---|
| 71 | | - | ] |
|---|
| 71 | | - | } |
|---|
| 71 | | - | |
|---|
| 71 | | - | // TEST VIEW |
|---|
| 71 | | - | function(doc) { |
|---|
| 71 | | - | if (doc.split) emit(null,doc.split); |
|---|
| 71 | | - | } |
|---|
| 71 | | - | |
|---|
| 71 | | - | // OUTPUT OF TEMP VIEW |
|---|
| 71 | + | OUTPUT OF TEMP VIEW |
|---|
| ... | |
|---|
| 139 | 139 | | { |
|---|
| 140 | 140 | | "total_rows": 1, |
|---|
| 141 | 141 | | "offset": 0, |
|---|
| ... | |
|---|
| 153 | 153 | | ] |
|---|
| 154 | 154 | | } |
|---|
| 155 | 155 | | |
|---|
| ... | |
|---|
| 156 | | - | //OUTPUT OF SAME VIEW AFTER SAVE |
|---|
| 156 | + | OUTPUT OF SAME VIEW AFTER SAVE |
|---|
| ... | |
|---|
| 157 | 157 | | { |
|---|
| 158 | 158 | | "total_rows": 1, |
|---|
| 159 | 159 | | "offset": 0, |
|---|
| ... | |
|---|
| 166 | 166 | | } |
|---|
| 167 | 167 | | ] |
|---|
| 168 | 168 | | } |
|---|
| ... | |
|---|
| 169 | + | |
|---|
| 169 | + | It seems from the above that couch is for some reason returning the splits field as an empty array. |
|---|
| 169 | + | |
|---|
| 169 | + | Thanks for your time on IRC already. |
|---|
| 169 | + | |
|---|
| 169 | + | Cheers |
|---|
| 169 | + | |
|---|
| 169 | + | John |
|---|
| ... | |
|---|