Skip to content

Instantly share code, notes, and snippets.

@SCdF
Last active June 6, 2018 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SCdF/0ea461584220b5ff2eed1a0220778517 to your computer and use it in GitHub Desktop.
Save SCdF/0ea461584220b5ff2eed1a0220778517 to your computer and use it in GitHub Desktop.
Reduce why you so confusing

Reduce why you so confusing

I have a bunch of docs that like this:

{
    "_id": "some-report-id",
    "reported_date": 123456,
    "patient_id": "abc-123"
}

And I have a map function that looks like this:

function(doc) {
    if (some_qualifiers_that_do_not_matter()) {
        emit([doc.patient_id], doc.reported_date);
    }
}

If you call the view with reduce=false you get the following:

{"total_rows":23,"offset":0,"rows":[
{"id":"578cfee7-524f-4287-8ce7-cce39534bc3a","key":["05E9B13F-F6F0-3D83-AC06-841DA01500E9"],"value":1527476718069},
{"id":"487b0e91-6bb8-4657-a433-5e831cf2266b","key":["0ba96f94b93899b06a4136ab5c62ce1f"],"value":1527476994787},
{"id":"afba0445-fefd-4fab-b59a-daaac0803963","key":["2515675d1644f1bd5f1ce794b3397088"],"value":1527886334235},
{"id":"0d6774b2-88e0-48d3-b6ee-e3e954b40ea2","key":["2515675d1644f1bd5f1ce794b3bba62d"],"value":1527636221711},
{"id":"19a68bba-8508-46f9-8b96-a5d63b4a9877","key":["2515675d1644f1bd5f1ce794b3bba62d"],"value":1527634753746},
{"id":"347b3251-5f32-4ea8-86ee-844032fd01d7","key":["2515675d1644f1bd5f1ce794b3bba62d"],"value":1527635523674},
{"id":"9a479fba-b627-4fe9-9494-42e3c09d59b0","key":["2515675d1644f1bd5f1ce794b3bba62d"],"value":1527805004432},
{"id":"f9f00c15-87cf-4123-a0c7-ea9527abe1fa","key":["2515675d1644f1bd5f1ce794b3bba62d"],"value":1527809279723},
{"id":"eb2f3e0f-ec32-44ea-93c1-a93e1de26132","key":["2515675d1644f1bd5f1ce794b3bdb3c8"],"value":1527628509518},
{"id":"f8f58fc2-5ace-4b8f-87f7-0849568b4fad","key":["2515675d1644f1bd5f1ce794b3c12814"],"value":1527475857712},
{"id":"252799d7-79a8-40f6-928c-d073765fed2f","key":["2c1c239f-4ddd-4c3b-aff2-147170ec0125"],"value":1523990477173},
{"id":"03fd5e71-9688-4e4d-8722-d587d16bc637","key":["2f74054d-c5c6-430f-90dc-5f25b6d2dec2"],"value":1524030713045},
{"id":"3819ff27-66c2-4c80-ae97-1132f599b8ad","key":["2f74054d-c5c6-430f-90dc-5f25b6d2dec2"],"value":1524030806213},
{"id":"085b125b-b1b0-4421-93bf-f0b106f6ebc8","key":["37dd9b74-bcae-4cb5-9461-e6283b6cb348"],"value":1527886297639},
{"id":"0998f812-ffac-46d0-86c7-719733e2e935","key":["5274e095-40ae-481c-b669-cb915c6765b9"],"value":1527885723636},
{"id":"251cb6a6-2c5f-4edb-947b-577c57b78b14","key":["6fc3d6a4-dc6e-47f1-9f61-dd4cd7069034"],"value":1527092110242},
{"id":"efe15dbd-cf99-4237-b724-eead57090515","key":["6fc3d6a4-dc6e-47f1-9f61-dd4cd7069034"],"value":1525297812343},
{"id":"2da71bc9-6e43-467d-8f29-abf1a37f35c8","key":["82c1c868215daa278f45209ab7299098"],"value":1527628575738},
{"id":"522aa11a-bce7-4606-9b30-0e63b6c727ec","key":["82c1c868215daa278f45209ab7299098"],"value":1527887889626},
{"id":"3be30ed6-e6fa-44d4-ab03-e4ec97e5d34c","key":["af074b4c-23ce-4870-b444-e3a1c98c3db3"],"value":1527885777743},
{"id":"f1943599-49cd-4835-a4a3-c458c1030952","key":["c0dd9c3b-31a0-44f2-9fd5-92f7d75446e1"],"value":1523989433303},
{"id":"1a57b408-a4f1-443a-9c96-1eacd364c367","key":["f25a1fe6-b9c1-4411-9de0-4a31aa800623"],"value":1527876099157},
{"id":"eb543ffe-f623-472a-a651-2470fd74ff20","key":["f25a1fe6-b9c1-4411-9de0-4a31aa800623"],"value":1527635956476}
]}

What I'm trying to do, is be able to query this view and get a list that is unique over the patient_id (i.e. the key from the map), where the value is the largest of the values for that patient_id.

For example, with 2515675d1644f1bd5f1ce794b3bba62d above there are 5 values:

1527636221711
1527634753746
1527635523674
1527805004432
1527809279723

The max being 1527809279723. So I'm aiming to see a call with the following line in it:

{"key": "2515675d1644f1bd5f1ce794b3bba62d", "value": 1527809279723 }

My reduce looks like this:

function(keys, values) {
  return Math.max(values);
}

If I call the view with ?reduce=true&group=true&group_level=1 I would expect that it would take the zeroth (or only) key value, group by that, and then pass the array of grouped values to reduce function.

What I get instead back is this:

{"rows":[
{"key":"05E9B13F-F6F0-3D83-AC06-841DA01500E9","value":1527476718069},
{"key":"0ba96f94b93899b06a4136ab5c62ce1f","value":1527476994787},
{"key":"2515675d1644f1bd5f1ce794b3397088","value":1527886334235},
{"key":"2515675d1644f1bd5f1ce794b3bba62d","value":null},
{"key":"2515675d1644f1bd5f1ce794b3bdb3c8","value":1527628509518},
{"key":"2515675d1644f1bd5f1ce794b3c12814","value":1527475857712},
{"key":"2c1c239f-4ddd-4c3b-aff2-147170ec0125","value":1523990477173},
{"key":"2f74054d-c5c6-430f-90dc-5f25b6d2dec2","value":0},
{"key":"37dd9b74-bcae-4cb5-9461-e6283b6cb348","value":1527886297639},
{"key":"5274e095-40ae-481c-b669-cb915c6765b9","value":1527885723636},
{"key":"6fc3d6a4-dc6e-47f1-9f61-dd4cd7069034","value":null},
{"key":"82c1c868215daa278f45209ab7299098","value":null},
{"key":"af074b4c-23ce-4870-b444-e3a1c98c3db3","value":1527885777743},
{"key":"c0dd9c3b-31a0-44f2-9fd5-92f7d75446e1","value":1523989433303},
{"key":"f25a1fe6-b9c1-4411-9de0-4a31aa800623","value":0}
]}

This doesn't seem possible! How are some null or 0!?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment