If you run an 'eval' query through 'mongo' on an authenticated database, you get the following:
1) providing wrong credentials
MacBook-Pro:mongotools me$ mongo --quiet --host 172.16.65.202 --port 27017 --username user --password pass --eval "printjson(db.adminCommand('listDatabases').databases)" test
Thu Jan 30 13:23:39.744 Error: 18
{ code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228
exception: login failed
MacBook-Pro:mongotools me$ echo $?
1
2) not trying to authenticate (missing user/password)
MacBook-Pro:mongotools me$ mongo --quiet --host 172.16.65.202 --port 27017 --eval "printjson(db.adminCommand('listDatabases').databases)" test
undefined
MacBook-Pro:mongotools me$ echo $?
0 <=== Incoherent
In authenticating with wrong credentials (1), you get the expected error code (non-zero) from the shell.
In not passing credentials (2), you get 'undefined' as the output, which is not really descriptive.
But the main issue is getting 0 as the exit code.
From a calling script, the result in providing no credentials or bad credentials should be the same.
Also, I think it should be 1, as the expected result is not sent.
Apart from being coherent with Unix commands, one scenario for which it is important to report a failure is the following:
- write script, test it, deploy it.
- authentication is added to the DB.
- the initial script, not using auth, should start failing by seeing a different return code.