我在mapreduce中有一组数据..收集随机formdata的1000000条记录.. 数据结构如下:
{ "_id" : ObjectId("4d9c8318cbb7813ef940d9e6"), "clientid" : 5, "FormData" : { "emailadress" : "SWV" } }
{ "_id" : ObjectId("4d9c8318cbb7813efb40d9e6"), "clientid" : 4, "FormData" : { "key1" : "VCYU", "key" : "PJO" } }
{ "_id" : ObjectId("4d9c8318cbb7813efc40d9e6"), "clientid" : 4, "FormData" : { "key1" : "NJ", "key" : "BZ" } }
{ "_id" : ObjectId("4d9c8318cbb7813efd40d9e6"), "clientid" : 2, "FormData" : { "last" : "AY", "first" : "B" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0041d9e6"), "clientid" : 4, "FormData" : { "key1" : "X", "key" : "QPIQ" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0241d9e6"), "clientid" : 5, "FormData" : { "emailadress" : "K" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0341d9e6"), "clientid" : 2, "FormData" : { "last" : "EJ", "first" : "K" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0441d9e6"), "clientid" : 5, "FormData" : { "emailadress" : "X" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0541d9e6"), "clientid" : 2, "FormData" : { "last" : "B", "first" : "G" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0641d9e6"), "clientid" : 4, "FormData" : { "key1" : "BWUE", "key" : "UJ" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0741d9e6"), "clientid" : 2, "FormData" : { "last" : "Q", "first" : "K" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0941d9e6"), "clientid" : 5, "FormData" : { "emailadress" : "IRH" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0b41d9e6"), "clientid" : 5, "FormData" : { "emailadress" : "VCMYE" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0c41d9e6"), "clientid" : 2, "FormData" : { "last" : "LZ", "first" : "L" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0d41d9e6"), "clientid" : 2, "FormData" : { "last" : "X", "first" : "YS" } }
{ "_id" : ObjectId("4d9c8318cbb7813e0e41d9e6"), "clientid" : 2, "FormData" : { "last" : "IA", "first" : "QT" } }
{ "_id" : ObjectId("4d9c8318cbb7813e1041d9e6"), "clientid" : 2, "FormData" : { "last" : "GZ", "first" : "MM" } }
{ "_id" : ObjectId("4d9c8318cbb7813e1141d9e6"), "clientid" : 3, "FormData" : { "phonenumber" : "G", "userid" : "KU", "key" : "KQ" } }
{ "_id" : ObjectId("4d9c8318cbb7813e1241d9e6"), "clientid" : 3, "FormData" : { "phonenumber" : "E", "userid" : "NU", "key" : "ZHP" } }
{ "_id" : ObjectId("4d9c8318cbb7813e1341d9e6"), "clientid" : 4, "FormData" : { "key1" : "FUYV", "key" : "GZ" } }
我的地图功能是
function () {
this.FormData.forEach(function (z) {emit(this.clientid, {count:1, datalength:1});});
}
我的缩减功能是:
function (key, vals) {
var result = {count:0, datalength:0};
vals.forEach(function (value) {result.count += value.count;result.datalength = value.datalength;});
return result;
}
当我运行db.clientdata.mapReduct(m,m2)时,我得到了
Wed Apr 6 15:09:35 uncaught exception: map reduce failed: {
"assertion" : "map invoke failed: JS Error: TypeError: this.FormData.forEach is not a function nofile_b:1",
"assertionCode" : 9014,
"errmsg" : "db assertion failure",
"ok" : 0
}
有人可以帮忙吗?
提前致谢
答案 0 :(得分:1)
forEach()
是Array
的方法,而不是任意对象文字。您无法使用它来迭代对象属性。
答案 1 :(得分:1)
如果它们具有相同的值,为什么使用count和datalength?好像你只需要使用计数。
地图功能:
function () {
emit(this.clientid, {count:1});
}
减少功能:
function (key, vals) {
var result = {count:0};
vals.forEach(function (value) {result.count += value.count;});
return result;
}