How to Unpretty Print in MongoDB – Quick Guide

This guide will explain to you the easiest way to unpretty print in MongoDB.

Even though MongoDB is easy to learn, straightforward to use, and packed with tons of features for developers, users can’t always control every aspect of the database.

If you know how to pretty print works in MongoDB then you will surely understand what I mean here by saying unpretty print in MongoDB. Now, pretty-printing is great when you want to see only a few documents systematically. However, when you want to list or view thousands of documents you cannot afford a pretty print there.

You can read the tutorial on pretty print in MongoDB here.

Well, this guide is the opposite – how to unpretty print in MongoDB. You must have noticed that methods or pipelines like sort in MongoDB by default print the output in a pretty format in the shell. A pretty format is when documents are printed on separate lines with proper indentation to them. This is done to better the readability of the cursor.

So, what is a cursor in MongoDB? Let us see.

What is a Cursor in MongoDB?

Whenever you use the find() method on a collection in a MongoDB database, the result is a list of documents. These are called cursors in MongoDB. This allows automatic iteration across the results of the query over these cursors.

An application developer could work with the cursor results directly in their application. A few cursor methods are available in MongoDB. They will be covered in different tutorials.

We can safely move on to working with unpretty print in MongoDB since we understand what a cursor in MongoDB is.

How to Unpretty Print in MongoDB?

Let us learn how to unpretty print in MongoDB. You must have noticed when you use the sort method the output is prettified by MongoDB. This is the default. To change this behavior and rather unpretty print in MongoDB, we can follow the simple steps below.

Unpretty Print in MongoDB with Nodejs

If you are performing a sort function or any other function that is resulting in a pretty output, you can simply pass the code below in a .js file and run it to unpretty print in MongoDB.

const cursor = db.collection.find().sort({_id:-1}).limit(4000);
while(cursor.hasNext()){
    printUnprettyJSON(cursor.next());
}

Run the file:

node file_name.js

Unpretty Print in Mongo Shell

Developers usually have the habit to apply the pretty() method when performing a find or read operation in the mongo shell. This is the cause behind the pretty print.

Let us look at what a prettified output looks like:

> db.shoes.find().pretty()
{
        "_id" : ObjectId("616b18a556e71503e4a0334d"),
        "name" : "Mariana Grider Boots",
        "price" : 23000,
        "size" : 7,
        "style" : "Slingbacks",
        "onSale" : false,
        "freeDelivery" : true,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334e"),
        "name" : "Purple Shimmery Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Peep Toes",
        "onSale" : true,
        "freeDelivery" : false,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334f"),
        "name" : "Zac Porter Straps",
        "price" : 19760,
        "size" : 8,
        "style" : "Ankle Strap",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a03350"),
        "name" : "Valerina's Red Carpet Toes",
        "price" : 26000,
        "size" : 9,
        "style" : "Scarpin",
        "onSale" : false,
        "freeDelivery" : false,
        "__v" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a03351"),
        "name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
        "price" : 10780,
        "size" : 7,
        "style" : "Wedge Booties",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0
}

Now, we might want to see unpretty compact JSON data in our mongo shell. We can simply choose to not use the pretty() method. This is what our data looks like without the pretty() method:

> db.shoes.find()
{ "_id" : ObjectId("616b18a556e71503e4a0334d"), "name" : "Mariana Grider Boots", "price" : 23000, "size" : 7, "style" : "Slingbacks", "onSale" : false, "freeDelivery" : true, "__v" : 0 }

{ "_id" : ObjectId("616b18a556e71503e4a0334e"), "name" : "Purple Shimmery Heels", "price" : 13380, "size" : 6, "style" : "Peep Toes", "onSale" : true, "freeDelivery" : false, "__v" : 0 }

{ "_id" : ObjectId("616b18a556e71503e4a0334f"), "name" : "Zac Porter Straps", "price" : 19760, "size" : 8, "style" : "Ankle Strap", "onSale" : true, "freeDelivery" : true, "__v" : 0 }

{ "_id" : ObjectId("616b18a556e71503e4a03350"), "name" : "Valerina's Red Carpet Toes", "price" : 26000, "size" : 9, "style" : "Scarpin", "onSale" : false, "freeDelivery" : false, "__v" : 0 }

{ "_id" : ObjectId("616b18a556e71503e4a03351"), "name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots", "price" : 10780, "size" : 7, "style" : "Wedge Booties", "onSale" : true, "freeDelivery" : true, "__v" : 0 }

Although this data looks ugly, it is compact.

This way you can unprettily print in MongoDB.

Conclusion

You must have noticed that methods or pipelines like sort in MongoDB by default print the output in a pretty format in the shell. A pretty format is when documents are printed on separate lines with proper indentation to them. This is done to better the readability of the cursor.

Pretty printing is great when you want to see only a few documents systematically. However, when you want to list or view thousands of documents you cannot afford a pretty print there.

This guide will help you unpretty print data in MongoDB.

Noteworthy References

SO Answer

Aneesha S
Aneesha S
Articles: 174