Uploaded image for project: 'MongoDB Database Tools'
  1. MongoDB Database Tools
  2. TOOLS-1805

How to Pass arg from cmd line to execute .js in mongo

    • Type: Icon: Question Question
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.2.14
    • Component/s: None
    • Labels:
      None
    • Environment:
      Debian linux

      I am trying to execute a .js from command line passing an arg to mongo like this:

      $ mongo --eval 'var x="Test text"' insertArg.js

      insertArg.js looks like this:

      function insertArg; {
      use PathTrackerDB;
      var inputText = x;
      var r =

      {'history_text':inputText}

      db.remote_history.insert(r);
      }

      I run the command:
      $ mongo --eval 'var x="Test text"' insertArg.js

      and I get the following output on the console:
      MongoDB shell version: 3.2.10
      connecting to: test
      $

      Then I go into the mongo shell and do a db.remote_history.count() and the collection has not been updated with the new row. I get no information good or bad from the shell script.

      Any ideas what I might be doing wrong?

      Much appreciated!

      Thanks, Karen

            Assignee:
            kelsey.schubert@mongodb.com Kelsey Schubert
            Reporter:
            KarenJA Karen J. Allen [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: