Docs Menu

Customize the mongosh Prompt

On this page

  • Display Line Numbers
  • Display Database and Hostname
  • Display System Up Time and Document Count

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. To have a custom prompt persist through restarts, add the code for your custom prompt to .mongoshrc.js.

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>

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:

admin@centos0722:27502>

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 +
" > ";
}
←  Configure mongosh Using a Configuration FileEditor Mode →
Give Feedback
© 2022 MongoDB, Inc.

About

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