Open In App

How to Set Pretty Print as Default in the MongoDB Shell?

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In MongoDB, optimizing data readability and usability is important. Pretty print, a formatting option within MongoDB, significantly enhances the visual appeal and comprehension of query results displayed in the MongoDB shell.

In this article, We will learn about What is Pretty Print and understand some examples that help us to understand Pretty Print in-depth manner.

What is Pretty Print?

  • Pretty print is a formatting option used in MongoDB to improve the readability of query results displayed in the MongoDB shell.
  • When pretty print is enabled, the output is formatted in a more structured and visually appealing manner compared to the default compact format.
  • Instead of presenting data in a single line with minimal spacing, pretty print adds indentation and line breaks to organize the output, making it easier for users to parse and understand, especially when dealing with complex data structures or large databases.
  • The syntax is as follows:
// Replace 'yourCollectionName' with the name of your MongoDB collection
db.yourCollectionName.find().pretty();

Explanation:

  • db.yourCollectionName refers to the MongoDB collection you want to query. You need to replace ‘yourCollectionName’ with the actual name of your collection.
  • .find() is a MongoDB method used to query documents in a collection.
  • .pretty() is a method used to format the query results in a more readable and structured manner.

Let’s set up an Environment:

To understand How to Set Pretty Print as Default in the MongoDB Shell, we’re creating a collection called “prettyDemo” and inserting a document with a single field named “fieldName” and its corresponding value “fieldValue”. This helps us demonstrate the concept in detail.

Examples of How to Set Pretty Print as Default in the MongoDB Shell

Example 1

// inserting data
> db.prettyDemo.insertOne({
"ClientName": "Larry",
"ClientAge": 27,
"ClientFavoriteCountry": ["US", "UK"]
});
// data inserted
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a440de01f572ca0ccf5f2")
}
// inserting data
> db.prettyDemo.insertOne({
"ClientName": "Mike",
"ClientAge": 57,
"ClientFavoriteCountry": ["AUS", "UK"]
});
// data inserted
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a4420e01f572ca0ccf5f3")
}

Here is the query to call pretty() function :

//show data with the help of pretty() function
>db.prettyDemo.find().pretty();

The following is the output:

Example-1

Setting Pretty Print as Default

Explanation: In the above output, we Set Pretty Print as Default to the whole collection easily.

Example 2

Here, we created another collection for better understanding the concept. So, we’re creating a collection called “gfg” and inserting number of documents into the collection.

// inserting data
db.gfg.insertMany([
{
"productName": "Laptop",
"brand": "Dell",
"price": 1200
},
{
"productName": "Smartphone",
"brand": "Samsung",
"price": 800
},
{
"productName": "Headphones",
"brand": "Sony",
"price": 100
}
]);

//collection inserted
{
acknowledged: true,
insertedIds: {
'0': ObjectId('661182f9a57582b2fc0ca537'),
'1': ObjectId('661182f9a57582b2fc0ca538'),
'2': ObjectId('661182f9a57582b2fc0ca539')
}
}

Here is the query to call pretty() function :

//show data with the help of pretty() function
> db.gfg.find().pretty();

The following is the output:

Example-2

Setting Pretty Print as Default

Explanation: In the above output, we Set Pretty Print as Default to the whole collection easily.

Conclusion

Overall, using pretty print as the default output format in MongoDB can improve the way you work with data. It makes query results clearer and easier to analyze, which can help to better decision-making. This simple adjustment can enhance your MongoDB experience and help you get more value from your data.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads