mapreduce - View collation in Couchdb keeping key selection -


if have couchdb of status updates:

{   username: "jp",   update: "confused couchdb",   updated_at: 2013-05-10t08:30:00z }  {   username: "jp",   update: "blissfully unaware",   updated_at: 2012-05-09t08:30:00z }  {   username: "bob",   update: "talking nonsense",   updated_at: 2013-04-07t22:15:00z } 

i'd retrieve jp's latest update.

  1. i know can create map function select jp's updates:

    function(doc) {   emit(doc.username,doc); } 

    then query _view/by_username?key="jp" - have sort output myself find 1 recent date.

  2. i know can create map function order date , username:

    function(doc) {   emit([doc.username, doc.updated_at],doc); } 

    but need know time of update request updates jp (as far know, there's no _view/by_username?key=["jp",*] type request, match anything)

  3. i have tried using reduce function on top of (1) above looks this:

    function (keys, values, rereduce) {   var latest;    for(var in keys) {       if (latest === undefined || new date(values[a].updated_at).gettime() > new date(latest.updated_at).gettime()) {         latest = values[a];       }   }    return latest; } 

    but because documents i'm storing quite large, "not reducing fast enough" error - reduce function outputting half data goes in (ie. 2 incoming documents become 1 outgoing document), apparently enough trigger warning message.

i feel i'm missing here - able me out?

i maaaay have answered own question.

the error occurs when output of reduce function big in comparison input. on databases odd number of documents there must reduce done input single document - of course entail output being same size input: error.

if change reduce function in (3) emit document id, don't trigger error:

function (keys, values, rereduce) {   var latest;   for(var in keys) {       if (latest === undefined || new date(values[a].updated_at).gettime() > new date(latest.updated_at).gettime()) {         latest = {"_id": values[a]._id};       }   }    return latest; } 

but can't request include_docs=true on reduced views (despite #couchdb-914 3 years back!) separate request must made retrieve document details. holds use case, i'm still looking better answer.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -