We all have heard how to do pretty print in MongoDB. It is used to format the query results with a proper indentation and log more readable output. We usually do it in Mongo Shell using the pretty() function.
Even we have a dedicated article on it: Pretty Print in MongoDB, where we have shown you how to do it in MongoDB shell.
Pretty print is extremely helpful still in some events we purposely want to print documents without it. But you must have noticed that some methods or pipelines like sort in MongoDB by default print the output in a pretty format in the shell. So we have to manually do an unpretty print for that. Here comes this article.
In this article, we will see how to do unpretty print, not only in the shell but also when performing operations with Node.js that automatically return documents in pretty print.
Why Prefer Unpretty Printing?
The question is why we do have an article on this topic? Why does someone want to print the documents in an unpretty printing, i.e., unstructured way?
The answer is – When we do have thousands of documents and they get printed in formatted format, they take up space. Pretty Print also inserts lines and space to make the format look like JSON. Now this makes it hard to read the document without scrolling, and in the case of a lot of documents, scrolling can become annoying. So unpretty print can be useful in such places. Let’s see how to do it. Let’s get started with understanding Cursor in MongoDB.
Understanding 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.
For example, when we use the find() and we haven’t passed any query then it returns a cursor that points toward all the documents in a collection.
An application developer could work with the cursor directly in their application. It can be used to get or manipulate the result sets returned by a query. MongoDB has many cursor functions that can be used to perform various operations on a cursor object such as next(), forEach(), toArray(), stream(), etc. We have a dedicated article on them, click here if you want to read.
Note: For this specific article, we will only have to keep in mind that MongoDB does pretty print by manipulating the cursor. So for doing its opposite, i.e., unpretty print we also have to perform operations on the cursor.
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 created output is prettified by MongoDB. This is the default. To change this behaviour and rather unpretty print in MongoDB, we can follow the simple steps below.
When Using Node.js
If you are performing a sort function or any other function that is resulting in a pretty output, you can simply write the below and run it to unpretty print in MongoDB.
Below is the code to unpretty print in MongoDB:
const cursor = db.collection.find().sort({_id:-1}).limit(4000);
while(cursor.hasNext()){
printUnprettyJSON(cursor.next());
}
Here the first line is selecting all the docs from a collection and sorting them on the basis of the id field in descending order, -1 means descending and 1 means ascending. Now we have a while loop with a condition cursor.hasNext() which runs the while loop till the cursor has documents to retrieve. Finally our main function, i.e., printjson(cursor.next()) will print document values in unformatted JSON, i.e., unpretty print.
Run the code:
node file_name.js
When Using Mongo Shell
Developers usually have the habit of applying the pretty() when printing document values in MongoDB 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.
Below is the command to unpretty print in MongoDB:
> 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
Nowadays, MongoDB is heading toward becoming an advanced database. You must have noticed that some of its tools, methods or pipelines automatically print the output in a pretty format to the console. 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. So for that, you have to use some additional function to revert the formatting, which we have done in this article when using MongoDB with Node.js. For Mongo Shell, simply avoiding using pretty() will do the task of unpretty print. We hope you find this tutorial useful.