0
讓我們有一個 「error_test.js」 文件,其中包含:如何檢測MapReduce的錯誤
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)'
同樣的情況,「db.doEval(my_js)」方法在Java中,我們將「error_test.js」文件的內容放入「my_js」變量中:未檢測到mapreduce錯誤(未啓動excepcion,返回空值,「ok:1.0」出現在響應中)。
所以我的問題是:如何檢測mapreduce中的錯誤? (都在一個js文件,並在doEval()方法)
謝謝
你可以把這個在一個try catch塊或做任何你想做一旦檢測到錯誤 - 包括返回的一些錯誤代碼...... –
謝謝你,它的工作原理。我不知道「db.runCommand()」返回一個值(一個JSON文檔)。 事實上,我發現手冊後面幾頁沒有提到返回值: 1)db.runCommand():http://docs.mongodb.org/manual/reference/method/ db.runCommand/ 2)mongo Shell方法:http://docs.mongodb.org/manual/reference/method/ 也許在其他一些頁面中有提及,但我沒有找到它們。如果某個mongo網頁編輯器正在閱讀此評論,請在這些頁面上添加一些關於返回值的信息。謝謝 – freesoft