具有Mongo Java驱动程序的DB.eval() [英] DB.eval() with Mongo Java Driver
问题描述
我已经准备了一些要在Mongo上执行的脚本. 它可以从命令行按预期工作:
I've prepared some script to execute on Mongo. It works as expected from command line:
mongo 127.0.0.1:27017/dbName script.js
但是当我尝试在Java(scala)代码中移动它时,它不起作用
But when I tried to move it in java (scala) code it doesn't work with
db.eval(scriptContent)
有人用DB.eval()方法成功吗?
Does anybody succeed with DB.eval() method?
推荐答案
eval()
VS这样运行脚本
mongo 127.0.0.1:27017/dbName script.js
不相等. eval()
发送要在mongod实例上执行的脚本,而该命令使用驱动程序访问数据库.
are not equivalent. eval()
sends the script to be executed on a mongod instance, whereas the command uses the driver to access the db.
正如其他人所指出的,应避免使用eval()
,因为它有很多缺点,例如,无法在分片环境中工作,或者不允许在后台创建索引.
As pointed out by others, eval()
should be avoided as it has a lot of drawbacks, for instance, won't work on a sharded environment, or does not allow to create indexes in background.
哦!并回答这个问题,是的,我确实在Java中成功使用eval()
.但是,有些事情根本行不通;)
Oh! and to answer the question, yes I did succeed with eval()
in java. However, there are things that will simply not work ;)
这篇关于具有Mongo Java驱动程序的DB.eval()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!