Mongosh does not trap return code like mongo

Hi MongoDB Community,

I am fairly new to MongoDB. I have been upgrading some 4.4.6 databases to 5.0.1. We have a few Linux bash scripts which use the mongo command and rely on some return code checking in the bash shell which is returned from javscript via the quit() method. I was looking at converting them to use the mongosh command. What I am finding is that the mongosh command doesn’t pass the return code back to the shell.

With mongo:

$ mongo --quiet  --host localhost --port 27017 --username userabc -p xxxxxxxx --authenticationDatabase admin << EOF
> var rc = 1;
> quit(rc);
> EOF
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
$ echo $?
1
$

With mongosh:

$ mongosh --quiet  --host localhost --port 27017 --username userabc -p xxxxxxxx --authenticationDatabase admin << EOF
var rc = 1;
quit(rc);
EOF

test> var rc = 1;
1
test> quit(rc);
$ echo $?
0
$

I’ll stick with the mongo command for now but should the return code behaviour be consistent between mongo and mongosh? Or should be be using a different approach for passing return codes back to the bash shell?

Any advice would be greatly appreciated.

Many thanks,
KC

This is a bug for sure. I’ve opened https://jira.mongodb.org/browse/MONGOSH-917 to track it, we should be able to get to it fairly soon. Thank you for bringing this up!

1 Like

To add onto that: If you need a workaround, you should be able to use process.exit(code) to exit mongosh with a specific code.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.