javascript - Mongodb mapreduce iterate through object's key value pairs -


i have mongodb collection has following data :

{ "_id" : objectid("4da31b8b5ba19e3c11345a66"), "userid" : 4, "datekey" : "bibak", "balancekey" : "maiym" } { "_id" : objectid("4da31b8b5ba19e3c12345a66"), "userid" : 4, "datekey" : "qotwh", "balancekey" : "sfeyq" } { "_id" : objectid("4da31b8b5ba19e3c14345a66"), "userid" : 4, "datekey" : "tlwjj", "balancekey" : "rdkam" } { "_id" : objectid("4da31b8b5ba19e3c15345a66"), "userid" : 5, "emailadress" : "kbdijd" } { "_id" : objectid("4da31b8b5ba19e3c16345a66"), "userid" : 1, "accountname" : "kl", "weblink" : "gj", "note" : "kp" } { "_id" : objectid("4da31b8b5ba19e3c17345a66"), "userid" : 1, "accountname" : "wd", "weblink" : "sz", "note" : "cl" } { "_id" : objectid("4da31b8b5ba19e3c18345a66"), "userid" : 1, "accountname" : "ik", "weblink" : "ok", "note" : "hd" } { "_id" : objectid("4da31b8b5ba19e3c19345a66"), "userid" : 4, "datekey" : "ugbyh", "balancekey" : "voprx" } { "_id" : objectid("4da31b8b5ba19e3c1a345a66"), "userid" : 3, "userid" : "zbwd", "password" : "fzak", "key" : "qmee" } { "_id" : objectid("4da31b8b5ba19e3c1b345a66"), "userid" : 1, "accountname" : "gh", "weblink" : "my", "note" : "qu" } { "_id" : objectid("4da31b8b5ba19e3c1c345a66"), "userid" : 3, "userid" : "yzmw", "password" : "mvur", "key" : "yszc" } { "_id" : objectid("4da31b8b5ba19e3c1d345a66"), "userid" : 4, "datekey" : "liewf", "balancekey" : "thxyr" } { "_id" : objectid("4da31b8b5ba19e3c1e345a66"), "userid" : 4, "datekey" : "uiwoy", "balancekey" : "skokg" } { "_id" : objectid("4da31b8b5ba19e3c1f345a66"), "userid" : 4, "datekey" : "poykk", "balancekey" : "kzgdz" } { "_id" : objectid("4da31b8b5ba19e3c20345a66"), "userid" : 4, "datekey" : "lwnxw", "balancekey" : "vjxfc" } { "_id" : objectid("4da31b8b5ba19e3c23345a66"), "userid" : 4, "datekey" : "iymgo", "balancekey" : "rwbue" } { "_id" : objectid("4da31b8b5ba19e3c24345a66"), "userid" : 3, "userid" : "cjth", "password" : "yqcl", "key" : "pcdb" } { "_id" : objectid("4da31b8b5ba19e3c25345a66"), "userid" : 4, "datekey" : "obocn", "balancekey" : "xohwa" } { "_id" : objectid("4da31b8b5ba19e3c26345a66"), "userid" : 3, "userid" : "ehtq", "password" : "kbxv", "key" : "yamd" } { "_id" : objectid("4da31b8b5ba19e3c27345a66"), "userid" : 5, "emailadress" : "vysahk" } 

i need writing mapreduce functions generate string csv structure.. example if need data userid = 4, result be

datekey,balancekey\n bibak,maiym\n qotwh,sfeyq\n ...... 

i facing problem doing following .. since each userid has different data, need way go through key/value pairs in generic way .. pretty question how loop through objects parameters , values in order emit them . , in reduce function can concatenate them , add \n.

thanks

you can have csv values each document using following code. there 2 variants of map() function provided example. first 1 non-generic , helps understand concept. while one, @ end, generic. try running both of them , understand difference.

map function - not generic

var map = function() {       var outputstring =  "";       if (this.datekey){            outputstring += this.datekey;       }         outputstring += "," ;       if (this.balancekey){            outputstring += this.balancekey;       }         emit(this.userid, {outputstring:outputstring}); }; 

reduce function

var reduce = function(key,values){     overalloutputstring = "";     values.foreach(function(i){         overalloutputstring += i.outputstring+"\n";     });     return { outputstring:overalloutputstring}; }; 

performing m/r operation

var result  = db.items.mapreduce( map,                                   reduce,                                   {query:{userid:{$exists:true}},                                    out: {replace:"csv_dump"}                                   }); 

observing output

db.csv_dump.find(); 

map function - generic

var map = function() {       var outputstring =  "";       for(var prop in ) {         if (prop != "_id" && prop != "userid" ){             outputstring += this[prop]+",";         }       }       outputstring = outputstring.substring(0, outputstring.length-1); // removing last comma       emit(this.userid, {outputstring:outputstring}); }; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -