Javascript Mongodb mapreduce遍历对象';s键值对

Javascript Mongodb mapreduce遍历对象';s键值对,javascript,mongodb,mapreduce,Javascript,Mongodb,Mapreduce,我有一个mongodb集合,其中包含以下数据: { "_id" : ObjectId("4da31b8b5ba19e3c11345a66"), "USERID" : 4, "datekey" : "BIBAK", "balancekey" : "MAIYM" } { "_id" : ObjectId("4da31b8b5ba19e3c12345a66"), "USERID" : 4, "datekey" : "QOTWH", "balancekey" : "SFEYQ" } { "_id" :

我有一个mongodb集合,其中包含以下数据:

{ "_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" }
我需要帮助编写mapreduce函数来生成类似csv结构的字符串。。例如,如果我需要userid=4的数据,结果将是

datekey,balancekey\n
BIBAK,MAIYM\n
QOTWH,SFEYQ\n
......
我在做以下工作时遇到了一个问题。。因为每个userid都有不同的数据,所以我需要一种方法以通用的方式遍历这些键/值对。。因此,问题主要在于如何循环遍历对象参数并获取它们的值以发射它们。然后在reduce函数中,我可以连接它们并添加\n


谢谢

您可以使用以下代码为每个文档设置csv值。作为示例,提供了map()函数的两种变体。第一个是非泛型的,有助于理解概念。而最后一个是通用的。试着运行这两个程序,你就会明白其中的区别

映射功能-非通用

var map = function() {
      var outputString =  "";
      if (this.datekey){
           outputString += this.datekey;
      }  
      outputString += "," ;
      if (this.balancekey){
           outputString += this.balancekey;
      }  
      emit(this.USERID, {outputString:outputString});
};
减少功能

var reduce = function(key,values){
    overallOutputString = "";
    values.forEach(function(i){
        overallOutputString += i.outputString+"\n";
    });
    return { outputString:overallOutputString};
};
执行M/R操作

var result  = db.items.mapReduce( map,
                                  reduce,
                                  {query:{USERID:{$exists:true}},
                                   out: {replace:"csv_dump"}
                                  });
观察输出

db.csv_dump.find();
映射功能-通用

var map = function() {
      var outputString =  "";
      for(var prop in this ) {
        if (prop != "_id" && prop != "USERID" ){
            outputString += this[prop]+",";
        }
      }
      outputString = outputString.substring(0, outputString.length-1); // removing last comma
      emit(this.USERID, {outputString:outputString});
};
你读过吗?