Call MongoDB function from Java

I am trying to call a JavaScript stored function from the MongoDB Java driver.

I follow this guide to save the function on the database server, and I can call the function from the mongo shell and return the result.

However, I cannot figure out how to call the same function in Java?

According to this http://api.mongodb.org/java/current/com/mongodb/DB.html#doEval-java.lang.String-java.lang.Object...- there is a method called doEval

I also tried using it with this method:

 public static String callFunction() { try (MongoClient client = new MongoClient("localhost")) { com.mongodb.DB db = client.getDB("TestDB"); return db.doEval("echoFunction", 3).toString(); } } 

But when I call the method, this is what I get:

 { "retval" : { "$code" : "function (x) {\n return x;\n}"} , "ok" : 1.0} 

and I would expect to return number 3 in this case.

Another problem with the code above is that the client.getDB() method is deprecated. As I understand it, the new method to call is client.getDatabase() , and it returns a MongoDatabase object, but according to the API there is no method to execute the function.

So my question is: is it possible to execute a stored JavaScript function on a database server with Java and return the result of this function? And if possible, I would appreciate help in how to do this.

Thanks.

Edit:

According to the comment , the server js function call on mongodb from java :

"It seems getNextSequence is a function written in mongo javascript shell. Neither the database (mongod) nor the Java side knows that this function exists, and none of them can interpret the Javascript code that the function contains. You will have to redefine it in Java. "

The function I'm trying to implement is a bit more complicated than the example above - it should return a collection of documents and doesn't seem to work using the db.doEval method.

So, I think the comment is correct?

+5
source share
3 answers

Instead, you should do this:

  return db.doEval("echoFunction(3)").toString(); 

If you use just the function name in eval, you only reference the server side JavaScript variable, storing the function code. He does not fulfill it. When you use parentheses, you request the actual execution of the function. If you need to send something more complex than a number, I would suggest using a JSON serializer.

+1
source

You can do all this with the java driver.

 MongoClient mongoClient = new MongoClient(); MongoDatabase mdb = mongoClient.getDatabase("TestDB"); /* run this <code snippet> in bootstrap */ BsonDocument echoFunction = new BsonDocument("value", new BsonJavaScript("function(x1) { return x1; }")); BsonDocument myAddFunction = new BsonDocument("value", new BsonJavaScript("function (x, y){ return x + y; }")); mdb.getCollection("system.js").updateOne( new Document("_id", "echoFunction"), new Document("$set", echoFunction), new UpdateOptions().upsert(true)); mdb.getCollection("system.js").updateOne( new Document("_id", "myAddFunction"), new Document("$set", myAddFunction), new UpdateOptions().upsert(true)); mdb.runCommand(new Document("$eval", "db.loadServerScripts()")); /* end </code snippet> */ Document doc1 = mdb.runCommand(new Document("$eval", "echoFunction(5)")); System.out.println(doc1); 

Result also:

 Document{{retval=5.0, ok=1.0}} 
+3
source

I solved the same problem as follows:

I run a command in mongoShell to create my saved JavaScript functions:

 db.system.js.save( { _id: "echoFunction" , value : function(x1) { return x1; } } ) db.system.js.save( { _id : "myAddFunction" , value : function (x, y){ return x + y; } } ); db.system.js.save( { _id: "fullFillCollumns" , value : function () { for (i = 0; i < 2000; i++) { db.numbers.save({num:i}); } } } ); 

To perform these functions from the Java driver from MongoDB:

 MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase("databaseName"); db.runCommand(new Document("$eval", "fullFillCollumns()")); Document doc1 = db.runCommand(new Document("$eval", "echoFunction(5)")); System.out.println(doc1); Document doc2 = db.runCommand(new Document("$eval", "myAddFunction(5,8)")); System.out.println(doc2); 

I see that collection numbers were created and filled with values. In the IntellijIdea console, I see:

 Document{{retval=5.0, ok=1.0}} Document{{retval=13.0, ok=1.0}} 
0
source

All Articles