Map Reduce functions executes on shell but fails to compile via MapReduceCommand java api

StackOverflow https://stackoverflow.com/questions/9474581

  •  13-11-2019
  •  | 
  •  

Question

I am trying to run the following command via the mongo Java API - MapReduceCommand class. During debug time the MapReduceCommand mrc seems to hold the following.

{ "mapreduce" : "inColl" , "map" : "function mapf(){var vr = new Date(this.reg_d.getFullYear(),this.reg_d.getMonth(),this.reg_d.getDate());var key = {seg_dt:vr,com: this.com}emit(key,{count:1});}" , "reduce" : "function reducef(key, values){var r = {count:0};values.forEach(function(v){r.count += v.count;});return r;}" , "verbose" : true , "out" : { "reduce" : "outColl"} , "query" : { "tp" : { "$in" : [ 1 , 0]}} , "finalize" : "function finalizef(key, value){var r = {com:key.com,ou_ke:0,seg_dt:key.seg_dt};r.ou_ke += value.count ;return r;}"}

When I do a MapReduceOutput = inColl.mapreduce(mrc);, I get the following error

Mon Feb 27 23:39:49 [conn48] building new index on { 0: 1 } for >djkdb.tmp.mr.inColl_outColl_15_inc Mon Feb 27 23:39:49 [conn48] done for 0 records 0secs Mon Feb 27 23:39:49 [conn48] building new index on { _id: 1 } for >djkdb.tmp.mr.inColl_outColl_15_inc Mon Feb 27 23:39:49 [conn48] done for 0 records 0secs Mon Feb 27 23:39:51 [conn47] JS Error: SyntaxError: missing ; before statement nofile_b:0 Mon Feb 27 23:39:51 [conn47] compile failed for: function mapf(){var vr = new >Date(this.reg_d.getFullYear(),this.reg_d.getMonth(),this.reg_d.getDate());var key = >{seg_dt:vr,com: this.com}emit(key,{count:1});} Mon Feb 27 23:39:51 [conn47] mr failed, removing collection

Is this a javascript syntax issue or am I missing something fundamental here ?

Thanks!

Was it helpful?

Solution

Looks like you have a syntax error:

function mapf(){
    var vr = new Date(this.reg_d.getFullYear(),this.reg_d.getMonth(),this.reg_d.getDate());
    var key = {seg_dt:vr,com: this.com}  <--- syntax problem
    emit(key,{count:1});
}

You are missing an semi-colon on line 3.

    var key = {seg_dt:vr,com: this.com}

My tip-off here was the error message:

[conn47] JS Error: SyntaxError: missing ; before statement 

I would suggest using something to help with formatting your code. Simply adding appropriate carriage made the mistake quite clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top