How to Set Pretty Print as Default in the MongoDB Shell?
Last Updated :
15 Apr, 2024
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:
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:
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.
Similar Reads
How to Print to Console an Object in a MongoDB Script?
MongoDB queries can sometimes be slow, especially when dealing with large datasets or complex queries. So there are some methods or approaches that allow the developers to check the data and spot mistakes during the program run. In this article, we will learn about How to Print to Console an Object
3 min read
How to List all Databases in the Mongo Shell?
Knowing how to list databases in MongoDB is an important part of managing your data effectively. By using basic MongoDB shell commands, you can easily see what databases you have and understand their sizes. By using commands such as show dbs and db.stats() and users can gain valuable insights into t
4 min read
How to Perform the SQL Join Equivalent in MongoDB?
In database management, with the rise of NoSQL databases like MongoDB, the approach to handling data relationships has evolved. MongoDB's document-oriented nature and schema-less design provide a different perspective on joins compared to SQL. In this article, we'll learn about How to Perform the SQ
6 min read
How to List All Collections in the MongoDB Shell?
Managing collections is a fundamental task in MongoDB database administration. Knowing how to list all collections in the MongoDB shell is essential for understanding your database structure and managing your data effectively. In this article, we'll explore how to list all collections in the MongoDB
3 min read
How to Execute Mongo Commands Through Shell Scripts?
Database management is a difficult field and MongoDB is a useful NoSQL database in this area. Automation of tasks through shell scripts is the way of effectively utilizing the abilities of a shell program. In this article, we will learn about How to execute MongoMongo commands through shell scripts
3 min read
How to do a Full-Text Search in MongoDB using Mongoose
In MongoDB, performing a full-text search allows us to query and retrieve documents based on textual content matching certain criteria. When using Mongoose, an ODM (Object Data Modeling) library for MongoDB in Node.js, conducting full-text search operations can be efficiently achieved using the Mong
5 min read
How to Register and Call a Schema in Mongoose?
Mongoose is a powerful Object Data Modeling (ODM) library for MongoDB and Node.js. It provides a straightforward and schema-based solution to model our application data. Understanding how to properly define and work with Mongoose schemas is essential for efficient MongoDB management and data interac
5 min read
How to Use $set and $unset Operators in MongoDB
MongoDB is a NoSQL database that stores data in documents instead of traditional rows and columns found in relational databases. These documents, grouped into collections, allow for flexible data storage and retrieval. One of MongoDBâs key advantages is its ability to dynamically update documents us
6 min read
How to Change the Data Store Directory in MongoDB?
In MongoDB, data files are stored by default in the /data/db directory on Unix-like systems and \data\db on Windows. This default setting may not be suitable for all applications particularly large-scale ones or if the default drive lacks sufficient space. In this article, we will learn about How to
3 min read
Mongoose SchemaType.prototype.default() API
The Mongoose SchemaType.prototype.default() method of the Mongoose API is used on the SchemaType object. It allows us to set the default value for the field in schema. Using this method we can provide default value to any path in the schema in the form of literal values or functions. The value we pr
4 min read