Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-769

Mongosh command line .js file param causes error

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 0.13.2
    • Affects Version/s: 0.12.1
    • Component/s: CLI Module
    • 3
    • Iteration Chicago
    • Needed
    • Hide

      We introduced a new flag, --file (or -f for short). When --file is used, the following argument is always interpreted as a filename, i.e. the following are equivalent:

      1. mongosh mongodb://localhost/ foo.js bar.js
      2. mongosh mongodb://localhost/ --file foo.js --file bar.js

      This is relevant because the first argument can be either a filename or a connection string/database name/etc.

      Concretely:

      mongosh some.where → connects to mongodb://some.where/ and opens a shell
      mongosh some.js → connects to mongodb://localhost/ and runs some.js

      The reason for this is that mongosh treats the first positional argument as a file if and only if:
      1. --nodb is passed, or
      2. The first argument does not start with mongodb:// or mongodb+srv:// and ends in either .mongodb or .js.
      (This differs from the legacy shell in that .mongodb is new as a file extension here, and that the legacy shell also treated the first argument as a filename if it contained a dot after the first forward or backwards slash, which is a rule that mongosh does not honour.)

      With --file, this becomes unambiguous:

      mongosh -f some.where → connects to mongodb://localhost/ and runs some.where as a JS file
      mongosh -f some.js → connects to mongodb://localhost/ and runs some.js as a JS file

      Because this is unambiguous, we would generally prefer that the --file variant is used primarily in the docs, and that the mongosh <url> <files...> variant without --file is discouraged.

      Show
      We introduced a new flag, --file (or -f for short). When --file is used, the following argument is always interpreted as a filename, i.e. the following are equivalent: 1. mongosh mongodb://localhost/ foo.js bar.js 2. mongosh mongodb://localhost/ --file foo.js --file bar.js This is relevant because the first argument can be either a filename or a connection string/database name/etc. Concretely: mongosh some.where → connects to mongodb://some.where/ and opens a shell mongosh some.js → connects to mongodb://localhost/ and runs some.js The reason for this is that mongosh treats the first positional argument as a file if and only if: 1. --nodb is passed, or 2. The first argument does not start with mongodb:// or mongodb+srv:// and ends in either .mongodb or .js. (This differs from the legacy shell in that .mongodb is new as a file extension here, and that the legacy shell also treated the first argument as a filename if it contained a dot after the first forward or backwards slash, which is a rule that mongosh does not honour.) With --file, this becomes unambiguous: mongosh -f some.where → connects to mongodb://localhost/ and runs some.where as a JS file mongosh -f some.js → connects to mongodb://localhost/ and runs some.js as a JS file Because this is unambiguous, we would generally prefer that the --file variant is used primarily in the docs, and that the mongosh <url> <files...> variant without --file is discouraged.

      Mongosh command line with host or port param plus .js file causes error: 

      mongosh --host myhost xxx.js
      MongoshInvalidInputError: [COMMON-10001] If a full URI is provided, you cannot also specify --host or --port

      mongosh --port 12345 xxx.js
      MongoshInvalidInputError: [COMMON-10001] If a full URI is provided, you cannot also specify --host or --port

      mongosh xxx.js
      Current Mongosh Log ID: 60a3c628615f42b8afe92b3a
      Connecting to: mongodb://xxx.js:27017/test?directConnection=true

       

       

            Assignee:
            anna.henningsen@mongodb.com Anna Henningsen
            Reporter:
            paul.done@mongodb.com Paul Done
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: