[SERVER-38993] `mongo --eval` exits with status 0 on failure Created: 14/Jan/19 Updated: 27/Oct/23 Resolved: 14/Jan/19 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Shell |
| Affects Version/s: | 4.1.6 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Minor - P4 |
| Reporter: | Oleg Pudeyev (Inactive) | Assignee: | Kelsey Schubert |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Operating System: | ALL | ||||||||
| Participants: | |||||||||
| Description |
|
Trying this on a 4.1 server:
As you see, the operation returned {ok:0}, but the exit status of the mongo command is 0 indicating success. Trying a different expression:
And what if I try to execute more than one command?
This is interesting. The second command was run but now the output of the first command is not shown. Is the shell recognizing when it is given one command or more than one command and behaving in different ways? If the shell does specially treat the case of a single-command --eval, when this single command fails, the shell should exit with a non-zero status code. |
| Comments |
| Comment by Oleg Pudeyev (Inactive) [ 15/Jan/19 ] | |
|
OK that is fine, however I thought about what I should search for to figure out how to make mongo exit with an error on failure and couldn't come up with anything, therefore perhaps linking to the doc page mentioned from --help output would be a good idea. | |
| Comment by Kelsey Schubert [ 14/Jan/19 ] | |
|
oleg.pudeyev, I don't think that'd be the best place for this content since it could get pretty wordy. I've opened Thanks, | |
| Comment by Oleg Pudeyev (Inactive) [ 14/Jan/19 ] | |
|
That works well, thank you. Can assert.commandWorked be mentioned somewhere next to --eval option in the help? | |
| Comment by Max Hirschhorn [ 14/Jan/19 ] | |
If you'd like the mongo shell to exit with a non-zero return code based on the server's response, then you'll want to assert on the command response using assert.commandWorked() or similar. The ReferenceError from attempting to use console.log was uncaught and is why the mongo shell exited with a non-zero return code.
The mongo shell has special logic to display the last value that's stored in the magic __lastres__ variable after evaluating an expression. The way JavaScript's evaluation of statements behaves, this corresponds to the result of the last statement in the sequence being printed out. | |
| Comment by Oleg Pudeyev (Inactive) [ 14/Jan/19 ] | |
|
Workaround:
|