Syntax error Prettyprint in MongoDB shell as default?

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"
   ]
}
Updated on: 2019-07-30T22:30:25+05:30

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements