Differences Between require()
and load()
require()
和load()
之间的差异
require()
and load()
On this page本页内容
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 mongosh
mongosh
中的脚本类型
mongosh
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 withNode.js脚本,是用require()
, including npm packages.require()
加载的任何脚本,包括npm包。These scripts are always files.这些脚本始终是文件。
Availability of require()
and load()
require()
和load()
的可用性
require()
and load()
The require()
and load()
methods differ in availability depending on the type of script you are using.require()
和load()
方法的可用性不同,这取决于您使用的脚本类型。
In在mongosh
scripts, 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()
的文件路径
require()
and 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,在Node.js脚本中,require()
uses the standard Node.js module resolution algorithm, starting from the file where
require()
was called.require()
使用标准的Node.js模块解析算法,从调用
require()
的文件开始。
Load External Code in a mongosh
Script在mongosh
脚本中加载外部代码
mongosh
ScriptYou 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 anothermongosh
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
环境变量返回包含正在执行的文件的目录的绝对路径。ExampleTo load a要从另一个mongosh
script namedtest-suite.js
from anothermongosh
script, add the following line to your script:mongosh
脚本加载名为test-suite.js
的mongosh
剧本,请在脚本中添加以下行: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 wheremongosh
started._dername
变量指定绝对路径可以确保您正在加载的单独脚本不受外部因素的影响,例如mongosh
的起点。To load a Node.js script from a要从mongosh
script, use therequire()
method.mongosh
脚本加载Node.js脚本,请使用require()
方法。ExampleTo load the date-fns要从名为module from a
mongosh
script calledtest-suite2.js
, add the following lines to your script:test-suite2.js
的mongosh
脚本加载date-fns模块,请在脚本中添加以下行:
const localRequire = require('date-fns').createRequire(__filename);
const fileExports = localRequire('./test-suite2.js'); }Access to the访问mongosh
APImongosh
APImongosh
scripts can use themongosh
API.mongosh
脚本可以使用mongosh
API。Node.js scripts do not have access to theNode.js脚本无法访问mongosh
API.mongosh
API。
For example, the例如,db全局变量(用于显示当前数据库)在db
global variable (used to display the current database) is available inside ofmongosh
scripts.mongosh
脚本中可用。It is not available inside of Node.js scripts.它在Node.js脚本中不可用。Importantmongosh
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
特定的代码。