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

Support for load() method

    • Type: Icon: Story Story
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 0.9.0
    • Affects Version/s: None
    • Component/s: None
    • Needed
    • Hide

      The load() method works as it did in the old shell.

      One difference is that __filename and __dirname are supported in a Node.js-like fashion, i.e. inside a script that was loaded with load() __filename will refer to the current script’s filename and __dirname to the directory of that file.

      Show
      The load() method works as it did in the old shell. One difference is that __filename and __dirname are supported in a Node.js-like fashion, i.e. inside a script that was loaded with load() __filename will refer to the current script’s filename and __dirname to the directory of that file.

      mongosh currently allows users to load external JS files with the .load REPL command. This is, however, somewhat limited.
      For example, I can't .load a JS file that contains .load commands inside.

      The legacy mongo shell exposed a load() method that could be used anywhere, i.e. directly in the REPL as well as inside files loaded into the REPL. This provides a much better user experience as it gives developers the ability to modularize the code the load into the shell to customize its behavior.

      In mongosh we should provide a similar functionality.

      Acceptance criteria

      • mongosh includes a load('<path to file>') method to load an external JS file into the shell
      • When the load() method is called, the file passed as argument is loaded into the shell, parsed and executed
      • Loaded files can contain other calls to load() to load other stuff
      • load() calls are processed synchronously and the result of a load() of a file that loads other files inside is equivalent to a load() of a big file with the other loaded files copy-pasted in the same order as the related load() calls
      • if the file passed to load() is not found or contains errors, mongosh prints out an error
      • if a file loaded from a loaded file is not found or contains errors, mongosh prints out an error

      Open questions

      • Can we make load() work also in Compass and VS Code?

            Assignee:
            anna.henningsen@mongodb.com Anna Henningsen
            Reporter:
            massimiliano.marcon@mongodb.com Massimiliano Marcon
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: