7

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.

3
  • 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. Commented Apr 17, 2015 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 :- <br/> { "ok" : 0.0 , "errmsg" : "no such cmd: echoFunction(3)" , "code" : 59 , "bad cmd" : { "echoFunction(3)" : true}} Commented Apr 18, 2015 at 1:52
  • You cam find this answer on another thread. stackoverflow.com/questions/47093563/… Commented Nov 8, 2017 at 10:17

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Is using this has any disadvantage over programmatic execution of the same task?
6

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/

Comments

3

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.

Comments

2
@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.