Navigation

Configure mongosh

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.

Note

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:

config

Return the current value for <property>:

config.get( "<property>" )

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

config.set( "<property>", <value> )
Key
Type
Default
Description
displayBatchSize
integer
20
The number of items displayed per cursor iteration
enableTelemetry
boolean
true
Enables sending anonymized tracking and diagnostic data to MongoDB.
historyLength
integer
1000
The number of items to store in mongosh REPL's history file.
inspectCompact
integer or boolean
3

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.

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

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.
showStackTraces
boolean
false
Controls display of a stack trace along with error messages.

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
3>

Use a function like this one to display the database and hostname in the mongosh prompt:

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

The prompt will look like this:

admin@centos0722:27502>

Use a function like this one for a prompt that shows the system uptime and a count of documents across all collections in the current database.

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

The prompt will look like this:

Uptime:451563 Documents:58 >

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 } )
Uncaught:
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.

Important

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: https://docs.mongodb.com/mongodb-shell/
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": "customer@clients.com" } )

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": "customer@clients.com" } )

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

db.contacts.find( {"email": "customer@clients.com" } )

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

db.contacts.find( {"email": "customer@clients.com" } )
config.set( "redactHistory", "remove" )
db.contacts.find( {"email": "<email>" } )
config.set( "redactHistory", "remove-redact" )
Give Feedback

On this page

  • Syntax
  • Customize the Prompt
  • Behavior
  • Examples