The require() and load() methods include files and modules in your scripts for added functionality. However, require() and load() differ in their behaviors and availability.require()和load()方法包括脚本中的文件和模块,以增加功能。然而,require()和load()在行为和可用性方面有所不同。
Types of Scripts in mongoshmongosh中的脚本类型
You can use the following types of scripts with 您可以在mongosh:mongosh中使用以下类型的脚本:
mongoshscripts, which can be any of the following:脚本,可以是以下任何一种: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 withNode.js脚本,它是任何用require(), including npm packages. These scripts are always files.require()加载的脚本,包括npm包。这些脚本始终是文件。
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在mongoshscripts, bothrequire()andload()are available.mongosh脚本中,require()和load()都可用。In Node.js scripts, only在Node.js脚本中,只有require()is available.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在mongoshscripts: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,在Node.js脚本中,require()uses the standard Node.js module resolution algorithm, starting from the file whererequire()was called.require()使用标准的Node.js模块解析算法,从调用require()的文件开始。
Load External Code in a mongosh Script在mongosh脚本中加载外部代码
You can load external code in a 您可以在mongosh script file, such as an npm package or a separate mongosh script.mongosh脚本文件中加载外部代码,例如npm包或单独的mongosh剧本。
To load a要从另一个mongoshscript from anothermongoshscript, use the__dirnameenvironment variable. The__dirnameenvironment variable returns the absolute path of the directory containing the file being executed.mongosh脚本加载mongosh脚本,请使用__dirname环境变量。__dirname环境变量返回包含正在执行的文件的目录的绝对路径。Example示例To load a要从另一个mongoshscript namedtest-suite.jsfrom anothermongoshscript, add the following line to your script:mongosh脚本加载名为test-suite.js的mongosh剧本,请在脚本中添加以下行:load(__dirname + '/test-suite.js')Using the使用_dirnamevariable to specify an absolute path ensures that the separate script you are loading is not affected by external factors such as wheremongoshstarted._dirname变量指定绝对路径可确保您正在加载的单独脚本不受外部因素的影响,例如mongosh的起始位置。To load a Node.js script from a要从mongoshscript, use therequire()method.mongosh脚本加载Node.js脚本,请使用require()方法。
require() Packaging Considerationsrequire()包装注意事项
There are two packaging standards for Node.js modules.Node.js模块有两种打包标准。
require() | |
|---|---|
CommonJS (CJS) | Yes |
ECMAScript Module (ES Module) | No |
You cannot 你不能在require() an ES module in mongosh. If you want to use functionality from an ES module, check to see if there is a CommonJS version that you can use instead. For more information, see:mongosh中require()一个ES模块。如果你想使用ES模块的功能,请检查是否有可以使用的CommonJS版本。有关更多信息,请参阅:
Access to the mongosh API访问mongosh API
mongoshscripts can use the脚本可以使用mongoshAPI.mongoshAPI。Node.js scripts do not have access to theNode.js脚本无法访问mongoshAPI.mongoshAPI。
For example, the 例如,db global variable (used to display the current database) is available inside of mongosh scripts. It is not available inside of Node.js scripts.db全局变量(用于显示当前数据库)在mongosh脚本中可用。它在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 因此,如果在Node.js脚本中运行mongosh code inside of a Node.js script.mongosh代码,可能会观察到意外的结果。
Generally, you should not keep mongosh-specific code inside Node.js scripts.一般来说,你不应该在Node.js脚本中保留特定于mongosh的代码。