MongoDB MapReduce复合键问题

时间:2016-07-02 03:49:14

标签: mongodb mapreduce

首先,我是MongoDB的新手,我想用MongoDB进行大数据分析 在我的问题中,我有一个MonogoDB集合,其中包含以下数据

 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

我非常感谢您帮助解决此问题

1 个答案:

答案 0 :(得分: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"
      }
)