How to Customize your MongoDB Shell Prompt

Introduction

Most shells in Linux, OSX, or Windows show a prompt with some info like the current user, current direcory, or computer name like so:

ComputerName:currentFolder myuser$

But the MongoDB shell by default gives you none of this context information. All it gives you is:

>

For users working in MongoDB shell some context information could be extremely useful.

Prerequisites

  • You should have MongoDB installed and running.
  • It’s recommended that you create a database and collection to experiment with as you follow along.
  • Some command line experience is recommended.

Goal

We always like to start out with an explicit goal to make sure we hit our mark by the end of the tutorial. Our goal is to add the current user and current database into the MongoDB command prompt like so:

myUser:databaseName >

1. Locate your .mongorc.js File

Our first step will be to find a file called .mongorc.js or create it if it doesn’t exist. The .mongorc.js file is javascript that runs everytime a user logs into the MongoDB shell. You can set some configurations here such as setting the default database you’d like to use. The file is typically in your home folder. We are on a mac so ours is /Users/myuser/.mongorc.js.

Note: This is a hidden file so make sure you are not missing it. Use ls -a in Terminal to see all hidden files. If this file doesn’t exist you can create it.

2. Add a prompt() function

The prompt function returns what the prompt should be, so start by writing a simple test function to make sure it’s working. Add this to your .mongorc.js using your favorite text editor or IDE. Note that this is not grabbing the actual username or current database, currently they are hardcoded strings just for testing.

function prompt() {
    var username = "currentUser";
    var currentDatabase = "currentDatabase";
    return `${username}:${currentDatabase} >`;
}

3. Verify test prompt() working

To evalute if our test prompt worked we need to log out and back into mongo. Your mongo prompt should’ve changed to this:

currentUser:currentDatabase >

4. Customize prompt() function

Now that we have a proof of concept we want to have the prompt to have the actual username and current database. We can get the current database using db.getName(). We can get the current user by running: db.runCommand({connectionStatus: 1}) and pulling out the desired username. At the end of the function we return a string using the values you’ve obtained, typically followed by the > character.

function prompt() {

    var username = "";
    var user = db.runCommand({
        connectionStatus: 1
    }).authInfo.authenticatedUsers[0];

    if (!!user) {
        username = user.user;
    } else {
        username = "anonymous";
    }

    var database = db.getName();

    return `${username}:${database} >`;
}

5. Verify

In order to verify that our prompt is working we’ll need to exit mongo and start it again. This will kickoff the mongorc file to run and create our new prompt. When you restart mongo it should look something like this:

$ mongo
anonymous:test >

We are an anonymous user at this point so this is correct. Now log in as a user with `db.auth(,) to test if the username works.

> use demoDatabase
switched to db demoDatabase
anonymous:demoDatabase > db.auth("demoDatabaseUser","demo123")
1
demoDatabaseUser:demoDatabase >

Now our shell prompt shows user and current database. We have successfully reached our goal.

Conclusion

We showed how to customize your MongoDB shell prompt but what you customize it with is up to you and your imagination. We used username and current database because we thought those were two pieces of information that would be extremely helpful. There’s a myriad of things you can do with the mongorc file and it is definitely worth looking into. If you’d like to learn more about the options available in these functions, the MongoDB documentations is fairly straightforward and can answer many of your questions. We hope this tutorial was able to answer your question or provide you the snippet of code that you were looking for all along. Thank you for your time and if you have any feedback or questions do not hesitate to reach out to us.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed Redis,
MongoDB & Elasticsearch

Get Started

OR

Try CockroachDB
in Beta

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.