首先,我是MongoDB的新手,我想用MongoDB做大数据分析。
在我的问题中,我有一个mongodb集合,其中包含以下数据
db.EIRLogs2.find()
{ "_id" : ObjectId("57772d1f152d447caa142fbf"), "NAME" : "Kamal", "AGE" : 20, "MARKS" : 100 }
{ "_id" : ObjectId("57772d1f152d447caa142fc0"), "NAME" : "Kamal", "AGE" : 30, "MARKS" : 300 }
{ "_id" : ObjectId("57772d1f152d447caa142fc1"), "NAME" : "Sunil", "AGE" : 30, "MARKS" : 300 }
{ "_id" : ObjectId("57772d1f152d447caa142fc2"), "NAME" : "Sunil", "AGE" : 30, "MARKS" : 400 }
{ "_id" : ObjectId("57772d1f152d447caa142fc3"), "NAME" : "Kasun", "AGE" : 20, "MARKS" : 100 }
{ "_id" : ObjectId("57772d1f152d447caa142fc4"), "NAME" : "Kamal", "AGE" : 20, "MARKS" : 100 }
{ "_id" : ObjectId("57772d1f152d447caa142fc5"), "NAME" : "Kasun", "AGE" : 20, "MARKS" : 100 }
{ "_id" : ObjectId("57772d1f152d447caa142fc6"), "NAME" : "Sunimal", "AGE" : 30, "MARKS" : 100 }
{ "_id" : ObjectId("57772d1f152d447caa142fc7"), "NAME" : "Kamal", "AGE" : 20, "MARKS" : 600 }
我希望对上面的数据集应用MapReduce函数,得到如下的答案
{ "_id" : "Kamal", "AGE" : 20, "value" : 800 }
{ "_id" : "Kamal", "AGE" : 30, "value" : 300 }
{ "_id" : "Kasun", "AGE" : 20, "value" : 200 }
{ "_id" : "Sunil", "AGE" : 30, "value" : 700 }
{ "_id" : "Sunimal", "AGE" : 20, "value" : 100 }
我使用下面的代码得到上面的答案,但它返回一个错误
db.EIRLogs2.mapReduce(
function() {
emit( {this.NAME,this.AGE}, this.MARKS );
},
function(key,values){
return Array.sum(values)
},
{ out: "ETom" }
)
我得到了以下错误
Sat Jul 2 09:17:32 SyntaxError: missing : after property id (shell):1
我非常感谢你帮助解决这个问题
emit
中的对象文字不是有效表达式,因为您没有指定名称。下面的命令可以解决您的问题:
db.EIRLogs2.mapReduce.mapReduce(
function() {
emit(
{ name: this.NAME, age: this.AGE },
this.MARKS
);
},
function(key,values) {
return Array.sum(values)
},
{
out: "ETom"
}
)