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

MongoDBDatabaseBig Data Analytics

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"]}
raja
Published on 01-Apr-2019 15:05:20
Advertisements