问题
I have a set of data in mapreduce.. 1000000 records for random formdata collected.. The data structure is as follows :
{ "_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" } }
My map function is
function () {
this.FormData.forEach(function (z) {emit(this.clientid, {count:1, datalength:1});});
}
My reduce function is :
function (key, vals) {
var result = {count:0, datalength:0};
vals.forEach(function (value) {result.count += value.count;result.datalength = value.datalength;});
return result;
}
When i run db.clientdata.mapReduct(m,m2) i get
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
}
Can anyone please help?
Thanks in advance
回答1:
Why are you using count and datalength if they have the same value? Seems like you just need to use count.
Map Function:
function () {
emit(this.clientid, {count:1});
}
Reduce Function:
function (key, vals) {
var result = {count:0};
vals.forEach(function (value) {result.count += value.count;});
return result;
}
回答2:
forEach() is a method of Array
, not any arbitrary object literal. You cannot use it to iterate over object properties.
来源:https://stackoverflow.com/questions/5571980/mongodb-mapreduce-giving-an-error