Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-38993

`mongo --eval` exits with status 0 on failure

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Works as Designed
    • Affects Version/s: 4.1.6
    • Fix Version/s: None
    • Component/s: Shell
    • Labels:
      None
    • Operating System:
      ALL

      Description

      Trying this on a 4.1 server:

      speed% /usr/local/m/versions/4.1/mongo --port 27741 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )'
      MongoDB shell version v4.1.6-84-g00520c2e0b
      connecting to: mongodb://127.0.0.1:27741/?gssapiServiceName=mongodb
      Implicit session: session { "id" : UUID("11b04863-67f8-4363-ae0d-a3f343b39712") }
      MongoDB server version: 4.1.6-84-g00520c2e0b
      {
      	"operationTime" : Timestamp(1547495772, 1),
      	"ok" : 0,
      	"errmsg" : "Invalid command argument. Expected '4.2' or '4.0', found 3.4 in: { setFeatureCompatibilityVersion: \"3.4\", lsid: { id: UUID(\"11b04863-67f8-4363-ae0d-a3f343b39712\") }, $clusterTime: { clusterTime: Timestamp(1547495772, 1), signature: { hash: BinData(0, 0000000000000000000000000000000000000000), keyId: 0 } }, $db: \"admin\" }. See http://dochub.mongodb.org/core/4.0-feature-compatibility.",
      	"code" : 2,
      	"codeName" : "BadValue",
      	"$clusterTime" : {
      		"clusterTime" : Timestamp(1547495772, 1),
      		"signature" : {
      			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
      			"keyId" : NumberLong(0)
      		}
      	}
      }
      speed% echo $?                                                                                                           
      0
      

      As you see, the operation returned

      {ok:0}

      , but the exit status of the mongo command is 0 indicating success.

      Trying a different expression:

      speed% /usr/local/m/versions/4.1/mongo --port 27741 --eval 'console.log'                                                  
      MongoDB shell version v4.1.6-84-g00520c2e0b
      connecting to: mongodb://127.0.0.1:27741/?gssapiServiceName=mongodb
      Implicit session: session { "id" : UUID("ece2b4e4-14ed-4cee-ab59-3a176dc95793") }
      MongoDB server version: 4.1.6-84-g00520c2e0b
      2019-01-14T14:57:30.050-0500 E QUERY    [js] ReferenceError: console is not defined :
      @(shell eval):1:1
      2019-01-14T14:57:30.050-0500 E -        [main] exiting with code -4
      speed% echo $?
      252
      

      And what if I try to execute more than one command?

      speed% /usr/local/m/versions/4.1/mongo --port 27741 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } ); print(1)'
      MongoDB shell version v4.1.6-84-g00520c2e0b
      connecting to: mongodb://127.0.0.1:27741/?gssapiServiceName=mongodb
      Implicit session: session { "id" : UUID("7c6eb26b-19b6-4426-8df6-03d77b97c138") }
      MongoDB server version: 4.1.6-84-g00520c2e0b
      1
      

      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.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: