Native Methods in mongoshmongosh中的本地方法

The methods listed in this section are mongosh functional replacements for the native methods that were available in the legacy mongo shell. 本节中列出的方法是传统mongoShell中可用的原生方法mongosh函数替代。These methods are not exact replacements: output formats and some functionality may differ from the corresponding legacy methods.这些方法不是精确的替代方法:输出格式和某些功能可能与相应的传统方法不同。

In addition to these methods, the mongocompat snippet provides access to legacy mongo shell APIs.除了这些方法之外,mongocompat代码段还提供了对传统mongoShell API的访问。

Note注意

In the following table <path> and <filename> are strings and should be in quotes.在下表中,<path><filename>是字符串,应该在引号中。

// process.chdir( <path> )
process.chdir( "./data/incoming" )
Legacy Method Name传统方法名称Replacement替换
cat()

Returns the contents of the specified file返回指定文件的内容

fs.readFileSync( <filename>, 'utf8' )

The legacy useBinaryMode option is not supported. 不支持旧版useBinaryMode选项。Emulate the useBinaryMode = false option with:使用以下选项模拟useBinaryMode=false选项:

fs.readFileSync( <filename>, 'utf8' ).replace( /\r\n/g, '\n' )
cd()

Changes the current working directory to the specified path.将当前工作目录更改为指定路径。

process.chdir( <path> )
getHostName()

Returns the hostname of the system running mongosh.返回运行mongosh的系统的主机名。

os.hostname()
getMemInfo()

Returns a document that reports memory used by the shell.返回一个报告shell使用的内存的文档。

process.memoryUsage()
hostname()

Returns the hostname of the computer running the shell.返回运行shell的计算机的主机名。

os.hostname()
isInteractive()

Returns a boolean indicating whether mongosh is running in interactive or script mode.返回一个布尔值,指示mongosh是以交互模式还是脚本模式运行。

isInteractive()
listFiles()

Returns an array of documents that give the name and type of each object in the directory.返回给定目录中每个对象的名称和类型的文档数组。

fs.readdirSync( <path>, { withFileTypes: true } )
load()

Loads and runs a JavaScript file in the shell.在shell中加载并运行JavaScript文件。

load() is available in mongosh. load()mongosh中可用。See also Differences Between require() and load().请参阅require()load()之间的差异

ls()

Returns a list of the files in the current directory.返回当前目录中的文件列表。

fs.readdirSync( <path> )
md5sumFile()

Returns the md5 hash of the specified file.返回指定文件的md5哈希。

crypto.createHash( 'md5' ).update( fs.readFileSync( <filename> ) ).digest( 'hex' )
mkdir()

Creates a directory at the specified path.在指定路径创建目录。

fs.mkdirSync( <path>, { recursive: true } )
pwd()

Returns the current directory.返回当前目录。

process.cwd()
quit()

Exits the current shell session.退出当前shell会话。

quit()
removeFile()

Removes the specified file from the local file system.从本地文件系统中删除指定的文件。

fs.unlinkSync( <filename> )
sleep()

Sleep for the specified number of milliseconds.睡眠指定的毫秒数。

sleep( <number> )
version()

Returns the current version of mongosh instance.返回mongosh实例的当前版本。

version()
_isWindows()

Returns true if the shell in running on Windows.如果shell在Windows上运行,则返回true

process.platform === 'win32'
_rand()

Returns a random number between 0 and 1.返回01之间的随机数。

Math.random()
←  SessionOptionsClient-Side Field Level Encryption Methods →