Forming a view/reduce for couchdb -
i'm new couchdb , concept of views , reduces, , not find me data in format want consume in.
my data - each set it's own document
{ "_id": "2012-10-28", "scores" : [ { "bob": 3, "dole": 5 } ] } { "_id" : "2012-10-29", "scores" : [ { "bob": 3, "dole": 6 } ] }
i view/reduce returns like:
"bob" : { "2012-10-27": 3, "2012-10-28": 3, ... }, "dole": { "2012-10-27": 5, "2012-10-28": 6, ... }
if not possible source data, can reorganize it, tough.
any appreciated. know of resources explain best practices views , reduces.
unless dates known , can hardcode them in reduce function, think it's bit difficult need map/reduce functions.
if ok output like:
{ "key": ["bob", "2012-10-27"], "value": {"score": 3} }
then map
function should work:
var scoresmapfn = function (doc) { var scores = doc.scores[0]; (var k in scores) { emit([k, doc._id], scores[k]); } };
note structures of original document optimised in opinion. have array scores have 1 element in object has several keys names/players). changed to:
{ "_id": "2012-10-28", "scores": [ { "name": "bob, "score": 3 }, { "name": "dole, "score": 5 } ] }
which make easier manipulate.
hope helps bit.
Comments
Post a Comment