Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Prettyprint in MongoDB shell as default?
You can call pretty() function on cursor object to prettyprint in MongoDB shell. The syntax is as follows −
db.yourCollectionName.find().pretty();
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
>db.prettyDemo.insertOne({"ClientName":"Larry","ClientAge":27,"ClientFavoriteCountry":["US","UK"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a440de01f572ca0ccf5f2")
}
>db.prettyDemo.insertOne({"ClientName":"Mike","ClientAge":57,"ClientFavoriteCountry":["AUS","UK"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a4420e01f572ca0ccf5f3")
}
Display all documents from a collection with the help of find() method. The query is as follows −
> db.prettyDemo.find();
The following is the output −
{ "_id" : ObjectId("5c8a440de01f572ca0ccf5f2"), "ClientName" : "Larry", "ClientAge" : 27, "ClientFavoriteCountry" : [ "US", "UK" ] }
{ "_id" : ObjectId("5c8a4420e01f572ca0ccf5f3"), "ClientName" : "Mike", "ClientAge" : 57, "ClientFavoriteCountry" : [ "AUS", "UK" ] }
Here is the query to call pretty() function −
> db.prettyDemo.find().pretty();
The following is the output −
{
"_id" : ObjectId("5c8a440de01f572ca0ccf5f2"),
"ClientName" : "Larry",
"ClientAge" : 27,
"ClientFavouriteCountry" : [
"US",
"UK"
]
}
{
"_id" : ObjectId("5c8a4420e01f572ca0ccf5f3"),
"ClientName" : "Mike",
"ClientAge" : 57,
"ClientFavoriteCountry" : [
"AUS",
"UK"
]
}Advertisements