本文介绍了如何使用Java MongoDriver执行MongoDB js脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这个例子.

当我使用mongo shell时,它工作正常,但是我想从Java程序内部运行它.这是代码:

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();
}

我不明白结果.

推荐答案

您应该使用DB.eval(),请参见 api文档,并确保您不进行字符串串联.而是通过变量.

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

我认为您的答案可能与 StackOverflow上的另一个答案相同

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

这篇关于如何使用Java MongoDriver执行MongoDB js脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:37