如何提前终止MongoDb shell脚本?

Gab*_*vay 10 javascript mongodb

写作:

if (...) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

在MongoDb shell脚本中会抱怨: SyntaxError: return not in function

我也尝试过exitmongo console命令:

if (...) {
    exit;
}
Run Code Online (Sandbox Code Playgroud)

但是你得到错误: ReferenceError: exit is not defined

如何才能提前终止js脚本文件中的执行?

NG.*_*NG. 16

在mongodb中,您可以使用此处quit()记录的功能.虽然它没有很好地记录,但我做了一个快速测试并且可以返回非零退出代码,这将退出w /状态1.quit(1)

  • 请注意,如果你要在Robomongo尝试它.它将立即关闭整个应用程序.[在OSX上测试,Robomongo 1.0.0-RC1] (3认同)
  • 发生在我身上@ŁukaszMazan,我在关闭后立即阅读了您的评论-.- (2认同)

Gab*_*vay 6

我使用以下解决方案:

(function(){

    // now you can use the return keyword anywhere:
    if (/* some condition */) {
        print("This is an error condition");
        return;
    }

})();
Run Code Online (Sandbox Code Playgroud)

我知道以这种方式终止脚本不会使mongo返回的错误代码不同于0(就像接受的解决方案一样)。