Docs Menu

Configure mongoshicons/link.png

On this page

  • Syntax
  • Customize the Prompt
  • Behavior
  • Examples

The config API provides methods to examine and update the mongosh configuration. Updates made using config persist between sessions.


The config API can be used within the mongosh command line interface. It has no effect in the embedded Compass shell.

Print the current mongosh configuration:


Return the current value for <property>:

config.get( "<property>" )

Change the current setting of <property> to <value>:

config.set( "<property>", <value> )
The number of items displayed per cursor iteration
Enables sending anonymized tracking and diagnostic data to MongoDB.
Designates an editor to use within the mongosh console. Overrides the EDITOR environment variable if set.
The number of items to store in mongosh REPL's history file.
integer or boolean

The level of inner elements that mongosh outputs on a single line. Short array elements are also grouped together on a single line.

If set to false, mongosh outputs each field on its own line.

integer or Infinity
The depth to which objects are printed. Setting inspectDepth to Infinity (the javascript object) prints all nested objects to their full depth.

Controls what information is recorded in the shell history. Must be one of:

  • keep: Retain all history.
  • remove: Remove lines which contain sensitive information.
  • remove-redact: Redact sensitive information.
Controls display of a stack trace along with error messages.
If true, automatically load installed snippets at startup.
A semicolon-separated list of URLs that link to a snippet registry.
The npm registry used by the mongosh npm client that installs snippet.

The prompt variable can store strings and JavaScript code to customize the mongosh prompt. Use a function that returns a string to display dynamic information in the prompt.

Custom prompts are not stored when you exit mongosh. Add the code for your custom prompt to .mongoshrc.js to set the prompt each time you start up mongosh.

This code will dynamically update the mongosh prompt to display line numbers:

let cmdCount = 1;
prompt = function() {
return (cmdCount++) + "> ";

The prompt will look like this:

1> show collections
2> use test

To display the database and hostname in the mongosh prompt, use a function like this one:

const hostnameSymbol = Symbol('hostname');
prompt = () => {
if (!db[hostnameSymbol])
db[hostnameSymbol] = db.serverStatus().host;
return `${db.getName()}@${db[hostnameSymbol]}> `;

The prompt will look like this:


To create a prompt that shows the system uptime and a count of documents across all collections in the current database, use a function like this one:

prompt = function() {
return "Uptime:" + db.serverStatus().uptime +
" Documents:" + db.stats().objects +
" > ";

mongosh makes "best-effort" attempts to match patterns that normally correspond to certain kinds of sensitive information.

There are patterns that match:

  • Certificates and keys
  • Generic user directories
  • Email addresses
  • IP addresses
  • URLs
  • MongoDB connection strings
  • Compass schema URL fragments
  • Electron application resources

Certain operations, such as connect(), are considered inherently sensitive. If redactHistory is set to remove or remove-redact, lines with these operations will be removed from the command line history.

Other operations, like find(), sometimes have sensitive information like email addresses. The shell history will retain these lines as entered unless redactHistory is set to remove-redact.

Consider viewing a collection with a number of large documents. You can update the batchSize to limit the number of items returned by a cursor.

config.set("displayBatchSize", 3)

Future db.collection.find() operations will only return 3 documents per cursor iteration.

Enable stack traces to see more detailed error reporting.

config.set("showStackTraces", true)

The output differs like this:

// showStackTraces set to 'false'
Enterprise> db.orders.find( {}, { $thisWontWork: 1 } )
MongoError: FieldPath field names may not start with '$'.
// showStackTraces set to 'true'
Enterprise> db.orders.find( {}, { $thisWontWork: 1 } )
MongoError: FieldPath field names may not start with '$'.
at MessageStream.messageHandler (/usr/bin/mongosh:58878:20)
at MessageStream.emit (events.js:315:20)
at MessageStream.EventEmitter.emit (domain.js:548:15)
at processIncomingData (/usr/bin/mongosh:57954:12)
at MessageStream._write (/usr/bin/mongosh:57850:5)
at writeOrBuffer (_stream_writable.js:352:12)
at MessageStream.Writable.write (_stream_writable.js:303:10)
at Socket.ondata (_stream_readable.js:719:22)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:548:15)

You can call the config API from the command line using --eval with mongosh. In this case the --nodb option means mongosh will update without connecting to a MongoDB database.


You must use different quotation marks for the --eval expression and the config property. That is, single quotes for one and double quotes for the other.

mongosh --nodb --eval 'config.set("enableTelemetry", true)'

mongosh returns additional information along with the result of the API call.

Current Mongosh Log ID: 609583b730e14918fa0d363f
Using MongoDB: undefined
Using Mongosh Beta: 0.12.1
For mongosh info see:
Setting "enableTelemetry" has been changed

Compare the recalled history when redactHistory is set to remove-redact or remove.

Set redactHistory to remove-redact mode and enter a query containing an email address.

config.set( "redactHistory", "remove-redact" )
db.contacts.find( {"email": "" } )

When you press the up arrow to replay the last command the email address is redacted.

db.contacts.find( {"email": "<email>" } ) // Redacted

Set redactHistory to remove mode and enter a query containing an email address.

config.set( "redactHistory", "remove" )
db.contacts.find( {"email": "" } )

When you press the up arrow to replay the last command the email address is present.

db.contacts.find( {"email": "" } )

The shell history reflects the changes. (Note that this stores the most recent input first.)

db.contacts.find( {"email": "" } )
config.set( "redactHistory", "remove" )
db.contacts.find( {"email": "<email>" } )
config.set( "redactHistory", "remove-redact" )
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


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