簡體   English   中英

Mongodb mapreduce迭代對象的鍵值對

[英]Mongodb mapreduce iterate through object's key value pairs

我有一個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
......

我正在面臨一個問題,如下所示。由於每個用戶標識都有不同的數據,我需要一種方法來以通用的方式遍歷這些鍵/值對。所以幾乎問題是如何循環遍歷對象參數並得到他們的價值,以發出它們。 然后在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});
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM