Database Manual / Reference / mongosh Methods

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. 本节中列出的方法是传统mongo shell中可用的本机方法的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代码片段还提供了对遗留mongo shell 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. Emulate the useBinaryMode = false option with:不支持旧版useBinaryMode选项。使用以下命令模拟useBinaryMode = false选项:

fs.readFileSync( <filename>, 'utf8' ).replace( /\r\n/g, '\n' )
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. See also Differences Between require() and load().load()mongosh中可用。另请参见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 } )
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()