gpt4 book ai didi

javascript - Mongodb mapreduce 遍历对象的键值对

转载 作者:可可西里 更新时间:2023-11-01 09:53:25 26 4
gpt4 key购买 nike

我有一个包含以下数据的 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
......

我在执行以下操作时遇到问题 .. 由于每个用户 ID 都有不同的数据,我需要一种方法以通用方式遍历这些键/值对 .. 所以几乎所有的问题都是如何遍历对象参数并获取它们的值以便发出它们。然后在 reduce 函数中我可以连接它们并添加\n。

谢谢

最佳答案

您可以使用以下代码为每个文档获取 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});
};

归约函数

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

关于javascript - Mongodb mapreduce 遍历对象的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5623626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com