How to terminate earlier a MongoDb shell script?

javascript, mongodb

Solution

In mongodb you can use the `quit()` function as documented here. While it's not documented well, I did a quick test and can return a non zero exit code by doing `quit(1)` which will exit w/ status 1.

Problem

Writing: ``` if (...) { return; } ``` in a MongoDb shell script will complain: `SyntaxError: return not in function` I have also tried the `exit` mongo console command: ``` if (...) { exit; } ``` but you get the error: `ReferenceError: exit is not defined` How can one earlier terminate the execution in the `js` script file?

Original source