-1

Suppose that you have java based server, and you are calling function which is stored in mongo db: Let say function name is test and impelemntaton is

function(arg1, arg2){
    return arg1;
}
DB db;
....

How to correctly pass the arguments? I've tryed simple pass them likewise

db.eval("test(arg1, arg2)", 1, 2); 

Unfortunately I am receiving error for wrong reference.

Simulant
  • 19,190
  • 8
  • 63
  • 98

1 Answers1

-1

I can not find stack trace, currently. But I've found the way how to bypass that exception. I've just created helper class with methods:

private static String constructCallStatment(String function) {
    String functionApi = extractApi(function);
    StringBuilder builder = new StringBuilder();
    builder.append("function(");
    builder.append(functionApi);
    builder.append(") { return ");
    builder.append(function);
    builder.append(";}");
    String retVal = builder.toString();
    return retVal;
}

public Object eval(String function, Object... args) {
    String callStatment = constructCallStatment(function);
    return mgDb.eval(callStatment, args);
}

public void setMgDb(DB mgDb) {
    this.mgDb = mgDb;
}

It helped me to call mongo db stored functions exactly from java side server.