i have saved two functions in the "system.js" collection under the names "mapfun" and "reducefun" and now i am attempting to invoke these functions from Java. I am trying to utilize MapReduceCommand for this purpose but encountering difficulties in calling them. Could someone provide assistance on this matter? The structure of the two functions is as follows: //mapfun
{ "_id" : "mapfun","value" : { "code" : "function(){var criteria; if(this.speed > 70 ){ criteria="overspeed";emit(criteria,this.speed); } }" } }
//reducefun
{ "_id" : "reducefun", "value" : { "code" : "function(key,speed){ var total=0; for(var i=0;i<speed.length;i++){ total=total+speed[i]; } return total/speed.length; }" } }
The code for my mapreducecommand is shown below:
MapReduceCommand command=new MapReduceCommand(collection, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput output=collection.mapReduce(command);
I have passed the map and reduce functions as strings wherein I attempted to call the mapfun and reducefun respectively as demonstrated below:
String map = "function (){var x=mapfun();"
+ "return x;};";
String reduce = "function(key, speed) {var y=reducefun(key,speed);"
+ "return y;};";
If there are any errors in my approach or if corrections need to be made, please advise me accordingly.