MongoDB MapReduce 迭代对象的键值对

3

我有一个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结构的字符串。例如,如果我需要用户ID为4的数据,则结果将是:

datekey,balancekey\n
BIBAK,MAIYM\n
QOTWH,SFEYQ\n
......

我在做以下操作时遇到了问题。由于每个用户ID都有不同的数据,我需要一种通用的方法来遍历这些键/值对。因此,问题基本上是如何循环遍历对象参数并获取它们的值以便发射它们。然后在reduce函数中,我可以将它们连接起来并添加\n。
谢谢。

你读过 http://www.mongodb.org/display/DOCS/MapReduce 吗? - Lucas Zamboulis
1个回答

1

您可以使用以下代码为每个文档获取csv值。提供了两个map()函数的变体作为示例。第一个是非通用的,有助于理解概念。而最后一个是通用的。尝试运行它们两个,您将理解它们之间的区别。

Map函数 - 非通用

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

Reduce 函数

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();

Map函数 - 通用

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});
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接