我有一个拥有以下数据的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。
由于
答案 0 :(得分:1)
您可以使用以下代码获取每个文档的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});
};