couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Darran White <darran.m.wh...@gmail.com>
Subject Select row with a count of 1 after reduce
Date Mon, 14 Jun 2010 22:53:32 GMT
Hi,
I have the following view:-
function(doc) {
    if(doc.doctype=='job'){
        emit([doc._id,doc.createdDate,'UNASSIGNED','CREATED'], doc._id);
     }else if(doc.doctype=='jobTask'){

emit([doc.jobId,doc.taskLatestDate,'ASSIGNED',doc.taskName],doc.jobId);
     }
}

So I have a Job and a JobTask the JobTask holds a reference to the Job in a
jobId field.
What I would like to do is select all the jobs which don`t have any
associated JobTasks.
I was thinking of using a simple reduce function :-

function(keys, values, rereduce) {
  return values.length;
}

So with a data set:
["0e482a01111e0c0932112f73c8001f68", "13-06-2010 20:42:22:613", "ASSIGNED",
"ACTIVE"]
["0e482a01111e0c0932112f73c8001f68", "13-06-2010 20:40:13:423",
"UNASSIGNED", "CREATED"]
["0e482a01111e0c0932112f73c80011ca", "13-06-2010 15:36:13:371",
"UNASSIGNED", "CREATED"]
["0e482a01111e0c0932112f73c800116a", "13-06-2010 19:20:48:541", "ASSIGNED",
"ACTIVE"]
["0e482a01111e0c0932112f73c800116a", "13-06-2010 15:35:59:912",
"UNASSIGNED", "CREATED"]
["0e482a01111e0c0932112f73c80005a5", "13-06-2010 19:20:48:507", "ASSIGNED",
"WIP"]
["0e482a01111e0c0932112f73c80005a5", "13-06-2010 15:33:02:947",
"UNASSIGNED", "CREATED"]

It would be reduced to:
["0e482a01111e0c0932112f73c8001f68"]    2
["0e482a01111e0c0932112f73c80011ca"]    1
["0e482a01111e0c0932112f73c800116a"]    2
["0e482a01111e0c0932112f73c80005a5"]    2

I would then like to select all those rows which have a count of 1 and load
the associated documents. However I`m not sure on how to select only those
which have a value of 1 or if this is possible.
I was thinking of maybe using a list function to only return those ids where
the value is 1 would this be the right way to approach this?

Another idea would be to get the latest date so

["0e482a01111e0c0932112f73c8001f68", "13-06-2010 20:42:22:613", "ASSIGNED",
"ACTIVE"]
["0e482a01111e0c0932112f73c8001f68", "13-06-2010 20:40:13:423",
"UNASSIGNED", "CREATED"]
["0e482a01111e0c0932112f73c80005a5", "13-06-2010 19:20:48:507", "ASSIGNED",
"WIP"]
["0e482a01111e0c0932112f73c80005a5", "13-06-2010 15:33:02:947",
"UNASSIGNED", "CREATED"]
["0e482a01111e0c0932112f73c80011ca", "13-06-2010 15:36:13:371",
"UNASSIGNED", "CREATED"]

Would return
["0e482a01111e0c0932112f73c8001f68", "13-06-2010 20:42:22:613", "ASSIGNED",
"ACTIVE"]
["0e482a01111e0c0932112f73c80005a5", "13-06-2010 19:20:48:507", "ASSIGNED",
"WIP"]
["0e482a01111e0c0932112f73c80011ca", "13-06-2010 15:36:13:371",
"UNASSIGNED", "CREATED"]

I could then filter on "CREATED"
I`m aware of the limit function but I need to get the latest entry for all
documents does any one have an idea on how to do this?

thanks

Darran

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message