Customizing MongoDB’s Shell with Compact Prompts


TL;DR: It's easy to customise the Mongo shell's prompt, especially if If you use MongoDB shell with one of MongoHQ's Elastic Deployments, you will have noticed that your replica set name is not as catchy as it could be. You will have noticed it because that replica set name appears in your Mongo shell prompt by default. So your prompt looks something like this...


That's a bit noisy and probably not telling you much unless you have excellent skills memorising and comparing hex strings. It is also telling you whether you are connected to the primary or secondary. Thats at least 37 characters and you are nearly half way across the screen before you start typing. What would probably be more useful is a shorter customised prompt that tells you what you want to know. Here we'll show you how in a couple of steps.

The prompt in Mongo shell is derived from a variable called prompt; if its not defined, then the shell shows us its default. The best place to set that is in your .mongorc.js file but before we do that let's see what we can do with it in the shell.

Losing the prompt

The first thing you might want to do is just strip down the prompt. To do that you can set prompt to a string:

set-5345738b13a3efb950000d32:PRIMARY> var prompt=">"  

If you aren't keen on the minimalism, just delete prompt to return to the default prompt. You can always set the variable to something more informative. Let's put the database name into the prompt:

>var prompt=db.getName()+">" exemplum>

The trouble with time

Of course we have to remember that the value of the prompt when set like that, is unchanging. So if we put the time in prompt like so:

exemplum>var prompt=ISODate().toLocaleTimeString()+">"  

it would always be 14:53:38. To have a dynamic prompt, we need to set prompt to a function, so that evaluating it will make the function return an newly calculated value.

14:53:38>var prompt=function(){ return ISODate().toLocaleTimeString()+">"; }  

Now the time will update when it displays the prompt. We're using the time here as its the most easily accessible changing value for all users. It could be any other statistic you want to display, but do remember that statistic will be calculated every time the prompt is displayed.

Making it stick

We're already getting to the point where we want to make this more permanant, so exit the shell and open an editor on your .mongorc.js file. You'll find it in your $HOME directory on Unix systems. We can set the prompt variable in there and create our own, ever smarter, version by adding this to .mongorc.js:

var prompt=function() {  
var dbname=db.getName();  
var master=db.isMaster().ismaster;  
var dblabel=master?dbname:"("+dbname+")"  
var time=ISODate().toLocaleTimeString();  
return dblabel+"/"+time+">";  

Here, the dbname is shown with parentheses around it if we are talking to a secondary node, and no parentheses if the primary, and adds in the local time to our compact prompt. That gives us a neat prompt:


And that replica set name we hid at the start? If you need that, just run db.isMaster().setName in the shell.

Now you can go and customise your prompt to what you need and get it displayed in the compact (or verbose) form you prefer. And when you've come up with your perfect custom prompt why not share it with us by mailing it to and we'll publish them in a future article.

Dj Walker-Morgan
Dj Walker-Morgan is Compose's resident Content Curator, and has been both a developer and writer since Apples came in II flavors and Commodores had Pets. Love this article? Head over to Dj Walker-Morgan’s author page and keep reading.