0

In MongoDB shell one can use db.loadServerScripts() command to load all scripts in the system.js collection for the current database. Then one can use functions in system.js collection for map-reduce or $where.

For example:

foo = function (parameters) .................foo code..................
db.system.js.save( { _id : "foo", value : foo } );

Now one can use foo in map-reduce.

How can I define a foo function to use it in a map-reduce using Java driver?

3
  • This basically never works how some people seem to expect it does. If you have code that actually "runs on the server" ( say via mapReduce or by eval() ) then you can reference that code and use it. Something like .loadServerScripts() only works in the shell because it is itself a JavaScript REPL, and therefore understands JavaScript. So this is reall of no use to you in another language, unless you similarly want to load in a JavaScript runtime. In all likelihood, the "how you intend to use your 'sum'" would also not be valid, and therefore you would code the real solution differently. Commented Feb 11, 2016 at 2:36
  • I edited my previous question. Now it is not a duplicate of what you suggested. At the most of one of my other questions: (stackoverflow.com/questions/34063323/…). Maybe you can edit the linked duplicate question. Commented Feb 14, 2016 at 3:19
  • How would this not be a duplicate? I see nothing in here that is asking anything really different, or more importantly does not end up with the same basic answer. You can of course "call" server stored JavaScript in $where or mapReduce, but you still need to send basic JavaScript code ( either special object for driver or just a string as argument, driver dependant ) as the basic "meat" of either the $where or mapReduce invocation itself. So for the driver driver ( according to documentation ) the arguments for JavaScript code would be a String. Commented Feb 14, 2016 at 5:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.