tags:

views:

69

answers:

1

I'm new to CouchDB and am just trying to assess its usefulness in common tasks. One such task is in generating reports. My problem is this: If I have a document structure like this:

{
   "_id": "29763f342ab34fd7b579fd4546aaed93",
   "_rev": "3-f56dccaa214f3e9fce1e1e3e32e710a2",
   "client_id": "sse",
   "outcomes": [
       {
           "contact": "phone",
           "type": "phone_outbound",
           "attempt": "1",
           "provider_id": "123456789",
           "status_outbound": "noanswer"
       },
       {
           "contact": "phone",
           "type": "phone_outbound",
           "attempt": "1",
           "provider_id": "123456789",
           "status_outbound": "noanswer"
       }
   ]
}

and a map function like this:

function(doc) {
    for(i=0;i<doc.outcomes.length;i++)
    {
        emit(null, {'client_id':doc.client_id,'outcome':doc.outcomes[i]});
    }
}

the result i get per generated row is:

{client_id: "sse", outcome: { contact: "phone", type: "phone_outbound", 
attempt: "1", provider_id: "123456789", status_outbound: "noanswer" }}

rather than

{client_id: "sse", contact: "phone", type: "phone_outbound", 
attempt: "1", provider_id: "123456789", status_outbound: "noanswer"}

see the extra 'outcome:' and parenthesis in the first example output? Thats what I dont want. Obviously my view is wrong but I cant work out how to achieve my goal. Can anyone assist? The key needs to remain null.

The reason I'm not referencing each field by name ie doc.contact is that I'd like to take advantage of the schemaless nature of it all. If I add an extra field to 'outcomes' i'd like to able to report on it without adding it explicitly to the map function.

+2  A: 

doc.outcomes[i].client_id = doc.client_id; emit(null, doc.outcomes[i]);

Marc Gear
perfect! thanks marc!
jdee