让我们有一个“error_test.js”文件,其中包含:
db = db.getMongo().getDB( "mydb" );
db.mycol.insert( { hello : "world" } );
print("it is shown");
db.runCommand(
{
mapReduce: "mycol",
map: function(){ print(not_exists); },
reduce: function(key, values){},
out: { replace: "myrescol" }
}
);
print("it is shown too (after error in mapreduce!)");
如果我运行该文件(在 Windows 命令行中),我得到:
mypath>mongo error_test.js
MongoDB shell version: 2.4.0
connecting to: test
it is shown
it is shown too (after error in mapreduce!)
mypath>echo %errorlevel%
0
mypath>
所以我们可以推断:
- mapreduce 错误不会停止执行。
- mapreduce 错误不会显示给用户。
- mapreduce 错误不会转换为退出代码(0 = 成功)(因此调用程序无法检测到错误)。
了解错误的唯一方法是在“mongod.log”中查找以下行:
Wed Jun 12 10:02:37.393 [conn14] JavaScript execution failed: ReferenceError: not_exists is not defined near '(){ print(not_exists)'
如果我们在 Java 中使用“db.doEval(my_js)”方法并将“error_test.js”文件的内容放入“my_js”变量中,也会发生同样的情况:未检测到 mapreduce 错误(没有启动异常,没有 null返回值,“ok : 1.0”出现在响应中)。
所以我的问题是:如何检测 mapreduce 中的错误?(在 js 文件和 doEval() 方法中)
谢谢