6

I have implemented a JavaScript function inside the Mongodb server using this example.

It works fine when I use mongo shell, but I want to run it from inside a Java program. This is the code:

public String runFunction() {

    CommandResult commandResult1 = db.command("db.loadServerScripts()");
    CommandResult commandResult2 = db.command("echoFunction(3)");

    return commandResult2.toString();
}

I don't understand the result.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Tharanga
  • 377
  • 3
  • 8
  • 18
  • What result do you get when you try it in mongo shell? What result do you get in Java? Maybe you should write another script that does both those commands, it appears that you are ignoring commandResult1. – Jim Apr 17 '15 at 22:50
  • When I use mongo shell first command is db.loadServerScripts().Then run echoFunction(3).It works fine, it print 3. When using java program I got this result :-
    **{ "ok" : 0.0 , "errmsg" : "no such cmd: echoFunction(3)" , "code" : 59 , "bad cmd" : { "echoFunction(3)" : true}}**
    – Tharanga Apr 18 '15 at 01:52
  • You cam find this answer on another thread. https://stackoverflow.com/questions/47093563/how-to-execute-mongodb-native-query-json-using-mongo-java-driver-only/47097555#47097555 – Charudatta Joshi Nov 08 '17 at 10:17

4 Answers4

6

The previous answers does not work in MongoDB 3.4+. Th proper way to do this in version 3.4 and above is to create a BasicDBObject and use it as the parameter of Database.runCommand(). Here's an example.

final BasicDBObject command = new BasicDBObject();
            command.put("eval", String.format("function() { %s return;}}, {entity_id : 1, value : 1, type : 1}).forEach(someFun); }", code));
            Document result = database.runCommand(command);
Prof Mo
  • 483
  • 5
  • 9
4

Since MongoDB 4.2 the eval command is removed (it was deprecated in 3.0). There is no more way to eval JavaScript scripts into MongoDB from the Java Driver.

See https://docs.mongodb.com/manual/reference/method/db.eval/

loicmathieu
  • 5,181
  • 26
  • 31
2

You should use DB.eval(), see the api docs and make sure that you don't do string concatenation. Pass the variables through instead.

I think your answer is probably the same answer as this other one on StackOverflow.

Community
  • 1
  • 1
Jim
  • 3,476
  • 4
  • 23
  • 33
1
@Autowired
private MongoOperations mongoOperations;

private final BasicDBObject basicDBObject = new BasicDBObject();

@PostConstruct
private void initialize() {
    basicDBObject.put("eval", "function() { return db.loadServerScripts(); }");
    mongoOperations.executeCommand(basicDBObject);
}

private void execute() {
    basicDBObject.put("eval", "function() { return echoFunction(3); }");
    CommandResult result = mongoOperations.executeCommand(basicDBObject);
}

And then you can use something like:

ObjectMapper mapper = new ObjectMapper();

And MongoOperation's:

mongoOperations.getConverter().read(CLASS, DBOBJECT);

Just try to have some workaround depends on your requirements

mrk3Stan
  • 11
  • 3