You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Simon Temple <si...@amalto.com> on 2017/01/24 16:51:29 UTC

CouchDB 2: Unique Id from view with custom list function

I have a problem very similar to that described in this stack overflow:  http://stackoverflow.com/questions/17017337/getting-unique-id-from-couchdb-view <http://stackoverflow.com/questions/17017337/getting-unique-id-from-couchdb-view>

So I have written a variation of the list function suggested as the answer that ensures unique ids.  It also sends the correctly formatted ViewResult JSON.

function ( head, req ) {

    var ar = new Array();
    var allRowsMap = {};

    while ( row = getRow() ) {
        ar.push( row.id );
        allRowsMap[row.id] = row;
    }

    ar = ar.sort().filter( function ( e, i, arr ) { return arr.lastIndexOf( e ) === i; } );

    send( '{"total_rows":' );
    send( ar.length );
    send( ',"offset":' );
    send( head.offset );
    send( ',"rows":[\n' );

    for ( i = 0; i < ar.length; i++ ) {
        send( toJSON( allRowsMap[ar[i]] ) );
        if ( (i + 1) < ar.length ) {
            send( ',' );
        }
        send( '\n' )
    }
    send( ']}' );
}

This is my view (all_by_realmOrgId) *without* the list function:

http://127.0.0.1:5984/mydb/_design/UserDoc/_view/all_by_realmOrgId <http://127.0.0.1:5984/mydb/_design/UserDoc/_view/all_by_realmOrgId>

{"total_rows":22,"offset":0,"rows":[
{"id":"4a4e63484222aba701b61117f3559478","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355b41d","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355b769","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355c095","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355c5b4","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355cd9d","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355dc76","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355dc95","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355e9fb","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355f1a1","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355f405","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f3559478","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355b41d","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355b769","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355c095","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355c5b4","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355cd9d","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355dc76","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355dc95","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355e9fb","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355f1a1","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355f405","key":["b2","2020202020202020"],"value":null}
]}

This is my view (all_by_realmOrgId) with the list function (unique_by_id):

http://127.0.0.1:5984/mydb/_design/UserDoc/_list/unique_by_id/all_by_realmOrgId <http://127.0.0.1:5984/mydb/_design/UserDoc/_list/unique_by_id/all_by_realmOrgId>

{"total_rows":11,"offset":0,"rows":[
{"id":"4a4e63484222aba701b61117f35096aa","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f352fa43","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f352fd3f","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f352ffe8","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f3530eaa","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f35311e0","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f3531e37","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f3532811","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f3533260","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f353396f","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f353483a","key":["b2","2020202020202020"],"value":null}
]}
However when I try and page my view it fails to work as I intended because it’s only listing uniquely the records selected between skip and limit:

http://127.0.0.1:5984/mydb/_design/UserDoc/_list/unique_by_id/all_by_realmOrgId?limit=6&skip=6 <http://127.0.0.1:5984/mydb/_design/UserDoc/_list/unique_by_id/all_by_realmOrgId?limit=6&skip=6>

{"total_rows":6,"offset":6,"rows":[
{"id":"4a4e63484222aba701b61117f3559478","key":["b2","2020202020202020"],"value":null},
{"id":"4a4e63484222aba701b61117f355dc76","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355dc95","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355e9fb","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355f1a1","key":["b2","1010101010101010"],"value":null},
{"id":"4a4e63484222aba701b61117f355f405","key":["b2","1010101010101010"],"value":null}
]}
.  This may be an impossible ask but…can anyone suggest a way to modify my list function to enable pagination to work?
.  Alternatively, is there another way to achieve the pagination of a view where duplicates can be removed (SELECT UNIQUE in SQL)


TIA


SimonT