Docs Menu

.mongoshrc Configuration File

On this page

  • Examples

On startup, mongosh checks the user's HOME directory for a JavaScript file named .mongoshrc.js. If this file is found, mongosh reads the content of .mongoshrc.js before displaying the prompt for the first time.

If you use mongosh to evaluate a JavaScript file or expression, either by using the --eval option on the command line or by specifying a .js file, mongosh reads .mongoshrc.js after the JavaScript has finished processing. You can prevent .mongoshrc.js from being loaded by using the --norc option.

Note

The legacy mongo shell used a configuation file called .mongorc.js. If mongosh detects this file on startup, and .mongoshrc.js is not present, mongosh will suggest renaming .mongorc.js to .mongoshrc.js.

The legacy .mongorc.js file is not loaded.

The following examples create custom prompts. To display:

add the example code 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 +
" > ";
}
←  Run Aggregation PipelinesConfigure Telemetry Options →

On this page

Give Feedback
© 2022 MongoDB, Inc.

About

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