How to Set Pretty Print as Default in the MongoDB Shell

Introduction

If you’re working with MongoDB through the shell you may find your eyes trying to sort through unreadable and jumbled data. You might already know but adding .pretty() command formats the results in a way that makes them much more readable. We asked ourselves how we can make this the default instead of having to add the .pretty() onto the command every time. We want to show you the way we found to do this in the hope that it saves you time like it did us. If this problem resonates with you please follow along as we go through step-by-step how to set pretty as the default.

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 for this tutorial is to make mongoDB shell print out .find() results in the .pretty() format without having to call .pretty() everytime.

Here’s an example of some unreadable results after a .find():

> db.users.find()
{ "_id" : ObjectId("5c7997e7ad685885625abce3"), "userId" : 1, "name" : "Al" }
{ "_id" : ObjectId("5c7997e7ad685885625abce4"), "userId" : 2, "name" : "Betty" }
{ "_id" : ObjectId("5c7997e7ad685885625abce5"), "userId" : 3, "name" : "Cameron" }

This example is not that hard to read but it is when there’s tons of data that doesn’t fit on one line. What we want it to look like is shown below with the .find().pretty():

> db.users.find().pretty()
{
        "_id" : ObjectId("5c7997e7ad685885625abce3"),
        "userId" : 1,
        "name" : "Al"
}
{
        "_id" : ObjectId("5c7997e7ad685885625abce4"),
        "userId" : 2,
        "name" : "Betty"
}
{
        "_id" : ObjectId("5c7997e7ad685885625abce5"),
        "userId" : 3,
        "name" : "Cameron"
}

As you can see this is more organized and easier to read.

1. Locate your .mongorc.js File

The first step is to locate your .mongorc.js file which is a javascript that runs everytime you log 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.

This file runs when user logs into shell and you can set pretty as default in this file. If this file doesn’t exist you can create it.

2. Set Pretty Print as Default in your .mongorc.js

When you’ve found your .mongorc.js file or created it, you’ll then want to add this line to it to make pretty print default. Use your favorite text editor.

DBQuery.prototype._prettyShell = true

You can also write directly to your .mongorc.js file through terminal using the echo command like so:

echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js

3. Verify

Lastly, we want to verify that it works. So let’s log into mongo shell and use a regular find command without pretty to see if pretty results are still returned:

> mongo
> use demoDatabase
> db.demoCollection.find()
> db.users.find()
{
        "_id" : ObjectId("5c7997e7ad685885625abce3"),
        "userId" : 1,
        "name" : "Al"
}
{
        "_id" : ObjectId("5c7997e7ad685885625abce4"),
        "userId" : 2,
        "name" : "Betty"
}
{
        "_id" : ObjectId("5c7997e7ad685885625abce5"),
        "userId" : 3,
        "name" : "Cameron"
}
>

As you can see, even when we didn’t tack on the .pretty command it still gave us pretty results.

Learn More

To learn more about the .mongorc.js we suggest you look up the MongoDB documentation directly or you can always reach out to an expert at Object Rocket to discuss your specific application. There are other interesting things you can do with this file such as setting the default database.

Conclusion

We showed you how to modify the .mongorc.js file to get pretty results by default. If you’d like to learn more about what you can do in this settings file, the MongoDB documentations is fairly straightforward and can answer many of your questions. We hope this tutorial was able to answer your question or set you on the right path to dong so. 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 CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

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.