How to make output of MongoDB’s find() readable in shell

MongoDB is one of the most popular NoSQL database where your data are stored in a collection. To quickly access your data from MongoDB you can execute commands on MongoDB shell and your results appear based on the given criteria. One of the commands is find() function and using this command you can easily retrieve data from MongoDB collection.

When you execute this find() command to retrieve data from a collection, it displays data in one line which makes it ugly and become difficult to read specially when there multiple rows are returned. To overcome the reading difficulty, I am going to show you how to make this output readable in an easy way. You just need to use pretty() function along with find() function.


MongoDB 4.4.0, MongoDB Setup

Insert Some Data

Let’s insert some sample data into the collection. First I will create a database called roytuts using the command use roytuts.

Next you need to also create a collection where you are going to insert or store your data. Use the below command to create collection name user.

db.runCommand( { create: "user" });

Now let’s insert the following data into the above collection from MongoDB shell or console. You need to use db for inserting into the collection user under database roytuts.


Once you execute above statements in the MongoDB shell, you should see the following output.

make output of mongodb find readable in shell

From the above output you see that each insert() statement was written successfully.

Now let’s retrieve data from collection user and check the output of the collection user using the following command.

make output of mongodb find readable in shell

What you see in the above output is it’s a bit difficult to read the values. It may not be so much problem for this output being small but think when you have a large dataset then it could be a very difficult to read the results of the find() command. Let’s make it prettify in the next section.

Prettify Output of find()

As a workaround MongoDB provides a function called pretty() which can be chained to the find() function. So execute the below command to get the output in pretty print format.


The above command now gives the following output.

make output of mongodb find readable in shell

This is all about pretty print of the find() output in MongoDB shell.

Leave a Reply

Your email address will not be published. Required fields are marked *