"Smoothing" a document hierarchy for reporting purposes

I am new to CouchDB and just trying to appreciate its usefulness in general tasks. One such task is to create 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 such a display function:

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

the result obtained in each generated line:

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

but not

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

see additional 'result:' and brackets in the first output example? That which I do not want. Obviously, my opinion is wrong, but I can’t understand how to achieve my goal. Can anyone help? The key must remain zero.

, , .. doc.contact, , . "", , .

+3
1

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

+2

Source: https://habr.com/ru/post/1731513/


All Articles