Pretty Print in MongoDB: What it is & How to Use it?

MongoDB is flexible, easy to learn and understand, and has loads of features for developers, but some things are quite ugly in their default state! do you understand?

Well, you guessed it, I am talking about those big and bottomless archive logs. Thankfully, MongoDB comes with a solution for this, which is the pretty() method.

In this tutorial, we will walk you through using pretty print in MongoDB i.e. the pretty() method, by illustrating examples of different use cases. We will consider a sample database with a collection having 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 default.

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

pretty() Method in MongoDB

Pretty printing can be done using the pretty() method in MongoDB. It simply makes the resulting documents cursors in MongoDB look readable and attractive, ultimately making them more legible. It is important to remember that pretty print in MongoDB is just a Mongo shell method. Using it is also straightforward.

Syntax:

Below is the syntax or prototype of Pretty Print.

database.collection.find(query).pretty()

where,

  • database is a database object,
  • collectionName is the name of a collection

pretty() Method works well with the cursor, so it is important to first understand it.

What is a Cursor in MongoDB?

A cursor pointed toward the documents which we tried to get. It can fetch the document in batches to reduce memory consumption and network bandwidth uses, as there can be millions of documents in a collection.

In the case of collection.find() method, if we haven’t passed any query then it returns a cursor that points toward all the documents of the respective collection.

We can use the pretty() method with a cursor to make the result look more readable and attractive.

We have a separate tutorial on the MongoDB cursor, click here if you want to read it.

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

Using Pretty Print in MongoDB

Let us use pretty print in MongoDB on sample database collections. We will use the find() method to get the default output and then compare it with the output generated by the pretty() method.

Prerequisites:

  • Start your MongoDB shell server.
  • List out all your databases and move into one of them.
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" : "[email protected]", "__v" : 2 }

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

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

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

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

{ "_id" : ObjectId("61576681ee49ac2db8adc6f1"), "products" : [ ], "name" : "Huckleberry Acres", "city" : "Nashville, Indiana", "email" : "[email protected]", "__v" : 0 }

So, here is the default output we see.

Now, use the pretty() method to see a more readable and cleaner output of the same cursor.

> db.farms.find({}).pretty()

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

See the difference? Beautiful, isn’t it? this is what exactly the pretty() method does in MongoDB.

Updating Documents then using Pretty Print

Let us see how we can update the documents and then use pretty print in MongoDB. The process is exactly the same, the purpose of this section is to give you another example and also see how beautiful it can make a single document.

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

{ "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" : "[email protected]", "__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" : "[email protected]",
        "__v" : 1
}

Setting Pretty Print in MongoDB as Default for Mongo Shell

Execute the below command in the mongo shell to set pretty print as the default.

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

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

Conclusion

In this tutorial, we learned to use the pretty() method to pretty print the documents returned by a cursor in MongoDB. We have also seen the command to set pretty print as default for Mongo Shell in MongoDB, make sure you execute this so that you don’t have to use the pretty() method again and again which is of course annoying. Hope you enjoy this tutorial.

Reference

https://stackoverflow.com/questions/9146123/pretty-print-in-mongodb-shell-as-default

Aneesha S
Aneesha S
Articles: 172