Pretty Print in MongoDB: What it is & How to Use it? [Ultimate Guide]

Pretty Print In MongoDB Featured Image

In this tutorial, I am going to explain what is pretty print in MongoDB and how you can use it.

Let’s face it, although MongoDB is flexible, easy to learn and understand, and comes with boatloads of features for developers, there are things that are pretty much ugly in their default state! You saw what I did there? Ha, great!

Well, you guessed it, I am talking about those large and unfathomable collections logs. Thankfully, MongoDB does come with a solution for it

Pretty print in MongoDB is denoted as a pretty() method. All it does is make the resulted documents of a Mongo shell query, also known as cursors in MongoDB, look readable and attractive, eventually making it more comprehensible. It is important to remember that pretty print in MongoDB is a mongo shell method only. It is also very simple to use.

In this guide I am going to walk you through using pretty print in MongoDB or the pretty() method by illustrating examples of different use cases. We will consider a sample database with a collection and a few documents added to it beforehand. We will also learn a few terminologies associated with this method and how we can set pretty print in MongoDB as the default behavior of the mongo shell.

So, let us get started with the ultimate guide to understanding pretty print in MongoDB.

Pretty Print in MongoDB Syntax

So, this is what the syntax or prototype looks like.

on.find().pretty()

What is a Cursor in MongoDB?

Cursors in a MongoDB database are nothing but the documents resulted from the mongo shell when you use the find() method on your collection. It allows iteration over these cursors automatically across the results of the query.

A developer could simply use the result set of the cursors to work with them in their application. MongoDB also provides a few methods for cursors. We shall discuss them in different tutorials.

Now that we understand what a cursor in MongoDB is, we can safely move on to work with pretty print in MongoDB.

Finding Documents & Using Pretty Print in MongoDB

Let us use the pretty print in MongoDB feature on a sample database and collection here. We shall use the find()method. We will also compare the outputs that are, the default and the ones resulting from the pretty() method.

Let us start with the steps:

  • Start your MongoDB shell server:
  • List out all your databases and move into one of it:
show dbs
use farmStandTake2
  • Now, from your preferred collection, look up documents using the find() method like this:
> db.farms.find({})

{ "_id" : ObjectId("6054f5397c7f6b1274b80939"), "products" : [ ObjectId("6054f5587c7f6b1274b8093a"), ObjectId("6055e998b1016a256057d538") ], "name" : "Lazy Sloth Holdings", "city" : "Fairfax, Virginia", "email" : "lzh@gmail.com", "__v" : 2 }

{ "_id" : ObjectId("6055ac0b6a275d2394ced121"), "products" : [ ], "name" : "Gospel Flat Farms", "city" : "Phuket City, Phuket Island", "email" : "gff@gmail.com", "__v" : 0 }

{ "_id" : ObjectId("605a2fa4c9fa2313b8c3bd40"), "products" : [ ObjectId("615763b1b579ab008019c4c3") ], "name" : "William's Ranch", "city" : "Kottayam, Kerala", "email" : "wr@hotmail.com", "__v" : 1 }

{ "_id" : ObjectId("605a31baa3e92a0f34774c88"), "products" : [ ObjectId("6157638db579ab008019c4c1") ], "name" : "Star Route Farms", "city" : "Bolinas, California", "email" : "srf@randommail.com", "__v" : 1 }

{ "_id" : ObjectId("61576604ee49ac2db8adc6f0"), "products" : [ ], "name" : "Scotman's Greenlands", "city" : "Stratford-upon-Avon, England", "email" : "scotmanslands@hotmail.com", "__v" : 0 }

{ "_id" : ObjectId("61576681ee49ac2db8adc6f1"), "products" : [ ], "name" : "Huckleberry Acres", "city" : "Nashville, Indiana", "email" : "huckleberryacres@farmerfriends.com", "__v" : 0 }

So, here is the default output we see.

  • Let us use pretty print in MongoDB now to see a more readable and cleaner output of the same result or cursor.
> db.farms.find({}).pretty()

{
        "_id" : ObjectId("6054f5397c7f6b1274b80939"),
        "products" : [
                ObjectId("6054f5587c7f6b1274b8093a"),
                ObjectId("6055e998b1016a256057d538")
        ],
        "name" : "Lazy Sloth Holdings",
        "city" : "Fairfax, Virginia",
        "email" : "lzh@gmail.com",
        "__v" : 2
}
{
        "_id" : ObjectId("6055ac0b6a275d2394ced121"),
        "products" : [ ],
        "name" : "Gospel Flat Farms",
        "city" : "Phuket City, Phuket Island",
        "email" : "gff@gmail.com",
        "__v" : 0
}
{
        "_id" : ObjectId("605a2fa4c9fa2313b8c3bd40"),
        "products" : [
                ObjectId("615763b1b579ab008019c4c3")
        ],
        "name" : "William's Ranch",
        "city" : "Kottayam, Kerala",
        "email" : "wr@hotmail.com",
        "__v" : 1
}
{
        "_id" : ObjectId("605a31baa3e92a0f34774c88"),
        "products" : [
                ObjectId("6157638db579ab008019c4c1")
        ],
        "name" : "Star Route Farms",
        "city" : "Bolinas, California",
        "email" : "srf@randommail.com",
        "__v" : 1
}
{
        "_id" : ObjectId("61576604ee49ac2db8adc6f0"),
        "products" : [ ],
        "name" : "Scotman's Greenlands",
        "city" : "Stratford-upon-Avon, England",
        "email" : "scotmanslands@hotmail.com",
        "__v" : 0
}
{
        "_id" : ObjectId("61576681ee49ac2db8adc6f1"),
        "products" : [ ],
        "name" : "Huckleberry Acres",
        "city" : "Nashville, Indiana",
        "email" : "huckleberryacres@farmerfriends.com",
        "__v" : 0
}

See the difference? Pretty, right? Well, this is what exactly pretty print in MongoDB does for you.

Updating Documents & Using Pretty Print in MongoDB

Let us now check out how we can update documents and then use pretty print in MongoDB.

  • Let us first update one registered farm’s email:
> db.farms.updateOne({ name: "William's Ranch" }, { $set: {email: "williamsranchofficial@wr.com"}})

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
  • Next, let’s see in the default output format whether it has changed:
> db.farms.find({ name: "William's Ranch"})

{ "_id" : ObjectId("605a2fa4c9fa2313b8c3bd40"), "products" : [ ObjectId("615763b1b579ab008019c4c3") ], "name" : "William's Ranch", "city" : "Kottayam, Kerala", "email" : "williamsranchofficial@wr.com", "__v" : 1 }
  • Let us see William’s Ranch using pretty print in MongoDB:
> db.farms.find({ name: "William's Ranch"}).pretty()

{
        "_id" : ObjectId("605a2fa4c9fa2313b8c3bd40"),
        "products" : [
                ObjectId("615763b1b579ab008019c4c3")
        ],
        "name" : "William's Ranch",
        "city" : "Kottayam, Kerala",
        "email" : "williamsranchofficial@wr.com",
        "__v" : 1
}

Setting Pretty Print in MongoDB as Default for Mongo Shell

Simply pass the below code snippet to set pretty print as default for your mongo shell.

echo DBQuery.prototype._prettyShell = true >> \.mongorc.js

Read More: Comprehensive Guide to Using the $ne Operator in MongoDB – 2021

Conclusion

Learn to use pretty print in MongoDB and setting it to default.

Noteworthy References