Docs HomeMongoDB Shell

Differences Between require() and load()require()load()之间的差异

The require() and load() methods include files and modules in your scripts for added functionality. require()load()方法在脚本中包含用于添加功能的文件和模块。However, require() and load() differ in their behaviors and availability.但是,require()load()的行为和可用性不同。

Types of Scripts in mongoshmongosh中的脚本类型

You can use the following types of scripts with mongosh:您可以将以下类型的脚本与mongosh一起使用:

  • mongosh scripts, which can be any of the following:mongosh脚本,可以是以下任意脚本:

    • Code entered directly into the REPL.直接输入REPL的代码。
    • The mongoshrc.js file.mongoshrc.js文件。
    • Code loaded with the load() method.使用load()方法加载的代码。
  • Node.js scripts, which are any scripts loaded with require(), including npm packages. Node.js脚本,是用require()加载的任何脚本,包括npm包。These scripts are always files.这些脚本始终是文件。

Availability of require() and load()require()load()的可用性

The require() and load() methods differ in availability depending on the type of script you are using.require()load()方法的可用性不同,这取决于您使用的脚本类型。

  • In mongosh scripts, both require() and load() are available.mongosh脚本中,require()load()都可用。
  • In Node.js scripts, only require() is available.在Node.js脚本中,只有require()可用。

File Paths for require() and load()require()load()的文件路径

The type of script determines how you specify file paths with require() or load().脚本的类型决定了如何使用require()load()指定文件路径。

  • In mongosh scripts:mongosh脚本中:

    • require() uses the standard Node.js module resolution algorithm, starting from the current working directory of the shell.require()使用标准的Node.js模块解析算法,从shell的当前工作目录开始。
    • load() takes either:采取以下任一项:

      • An absolute path, or绝对路径,或
      • A relative path. When using a relative path, the path is always interpreted as relative to the current working directory of the shell.相对路径。当使用相对路径时,该路径始终被解释为相对于shell的当前工作目录。
  • In Node.js scripts, require() uses the standard Node.js module resolution algorithm, starting from the file where require() was called.在Node.js脚本中,require()使用标准的Node.js模块解析算法,从调用require()的文件开始。
Tip

To return the current working directory of the shell, run the pwd() method from your script.要返回shell的当前工作目录,请从脚本中运行pwd()方法。

To change the shell's working directory, use the cd() method in your script.要更改shell的工作目录,请在脚本中使用cd()方法。

Load External Code in a mongosh Scriptmongosh脚本中加载外部代码

You can load external code in a mongosh script file, such as an npm package or a separate mongosh script.您可以在mongosh脚本文件中加载外部代码,例如npm包或单独的mongoh脚本。

  • To load a mongosh script from another mongosh script, use the __dirname environment variable. 要从另一个mongosh脚本加载一个mongosh脚本,请使用__dirname环境变量。The __dirname environment variable returns the absolute path of the directory containing the file being executed.__dirname环境变量返回包含正在执行的文件的目录的绝对路径。

    Example

    To load a mongosh script named test-suite.js from another mongosh script, add the following line to your script:要从另一个mongosh脚本加载名为test-suite.jsmongosh剧本,请在脚本中添加以下行:

    load(__dirname + '/test-suite.js')

    Using the _dirname variable to specify an absolute path ensures that the separate script you are loading is not affected by external factors such as where mongosh started.使用_dername变量指定绝对路径可以确保您正在加载的单独脚本不受外部因素的影响,例如mongosh的起点。

  • To load a Node.js script from a mongosh script, use the require() method.要从mongosh脚本加载Node.js脚本,请使用require()方法。

    Example

    To load the date-fns module from a mongosh script called test-suite2.js, add the following lines to your script:要从名为test-suite2.jsmongosh脚本加载date-fns模块,请在脚本中添加以下行:

    const localRequire = require('date-fns').createRequire(__filename);
    const fileExports = localRequire('./test-suite2.js'); }

Access to the mongosh API访问mongosh API

  • mongosh scripts can use the mongosh API.mongosh脚本可以使用mongosh API。
  • Node.js scripts do not have access to the mongosh API.Node.js脚本无法访问mongosh API。

For example, the db global variable (used to display the current database) is available inside of mongosh scripts. 例如,db全局变量(用于显示当前数据库)在mongosh脚本中可用。It is not available inside of Node.js scripts.它在Node.js脚本中不可用。

Important

mongosh scripts and Node.js scripts run in different contexts. mongosh脚本和Node.js脚本在不同的上下文中运行。They may exhibit different behaviors when the same command is run in each type of script, such as returning different data types. 当在每种类型的脚本中运行相同的命令时,它们可能会表现出不同的行为,例如返回不同的数据类型。Therefore, you may observe unexpected results if you run mongosh code inside of a Node.js script.因此,如果在Node.js脚本中运行mongosh代码,您可能会观察到意外的结果。

Generally, you should not keep mongosh-specific code inside Node.js scripts.一般来说,您不应该在Node.js脚本中保留mongosh特定的代码。