Docs Menu

Docs HomeMongoDB Manual

Native Methods in mongosh

The methods listed in this section are mongosh functional replacements for the native methods that were available in the legacy mongo shell. 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.

Note

In the following table <path> and <filename> are strings and should be in quotes.

// 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:

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

os.hostname()
getMemInfo()

Returns a document that reports memory used by the shell.

process.memoryUsage()
hostname()

Returns the hostname of the computer running the shell.

os.hostname()
isInteractive()

Returns a boolean indicating whether mongosh is running in interactive or script mode.

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.

load() is available in mongosh. See also Differences Between require() and load().

ls()

Returns a list of the files in the current directory.

fs.readdirSync( <path> )
md5sumFile()

Returns the md5 hash of the specified file.

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.

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.

version()
_isWindows()

Returns true if the shell in running on Windows.

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

Returns a random number between 0 and 1.

Math.random()
What is MongoDB? →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.