How to print to console an object in a MongoDB script?


You can use printjson() method to print to console an object in a MongoDB script. The syntax is as follows −

printjson({yourFieldName”:yourValue”,........N});

You can use JSON.stringify() along with print() function. The syntax is as follows minus;

print ( JSON.stringify( { {yourFieldName”:yourValue”,........N} } ));

Let us implement the above syntax to print object in Mongo script. The query is as follows −

>printjson({"UserId":101,"UserName":"John","UserCoreSuject":["Java","MongoDB","MySQL","SQL Server"]});

The following is the output −

{
   "UserId" : 101,
   "UserName" : "John",
   "UserCoreSuject" : [
      "Java",
      "MongoDB",
      "MySQL",
      "SQL Server"
   ]
}

You can use another query −

> print ( JSON.stringify( {"UserId":101,"UserName":"John","UserCoreSuject":["Java","MongoDB","MySQL","SQL Server"]} ));

The following is the output −

{"UserId":101,"UserName":"John","UserCoreSuject":["Java","MongoDB","MySQL","SQL Server"]}

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements