0

I have a problem executing stored function on MongoDB from my java code , i have a stored function like this.

db.system.js.save({

  _id:"myFunction",
  value:function(data){
    //todo        
    return 1 + 1;
   }
});

And in my java code :

mongoClient = new MongoClient(SERVER, PORT); // should use this always
db = mongoClient.getDB(DB);
DBObject datos = new BasicDBObject();
datos.put("val", "myvalue");

Then i am trying to execute the function:

try 1

db.doEval("myFunction", datos);

try 2

db.eval("myFunction", datos);

try 3

DBObject query = new BasicDBObject();
query.put("myFunction", datos);
db.command(query);

But the stored function does not execute, any ideas ?

1 Answer 1

1

I solved, it seems to pass values/objects to the stored function you have to serialize the object as JSON, so I did this :

String values = JSON.serialize(datos);

Then my java code looks like :

CommandResult er = db.doEval("myFunction("+values+")");

And the stored function gets executed correctly.

Sign up to request clarification or add additional context in comments.

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.