 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
How to print results of script in MongoDB?
We will use printjson() for this. Let us first create a collection with documents −
> dbprintResultScriptDemoinsertOne({"StudentName":"John","StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf22c02b64a577be5a2bc0b") } > dbprintResultScriptDemoinsertOne({"StudentName":"Carol","StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf22c09b64a577be5a2bc0c") } > dbprintResultScriptDemoinsertOne({"StudentName":"David","StudentAge":19}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf22c11b64a577be5a2bc0d") } Following is the query to display all documents from a collection with the help of find() method −
> dbprintResultScriptDemofind();
This will produce the following document −
{ "_id" : ObjectId("5cf22c02b64a577be5a2bc0b"), "StudentName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cf22c09b64a577be5a2bc0c"), "StudentName" : "Carol", "StudentAge" : 20 } { "_id" : ObjectId("5cf22c11b64a577be5a2bc0d"), "StudentName" : "David", "StudentAge" : 19 } Following is the query to print results of script −
> var document=dbprintResultScriptDemofind(); > while (documenthasNext()) {    printjson(documentnext()); } This will produce the following document −
{    "_id" : ObjectId("5cf22c02b64a577be5a2bc0b"),    "StudentName" : "John",    "StudentAge" : 21 } {    "_id" : ObjectId("5cf22c09b64a577be5a2bc0c"),    "StudentName" : "Carol",    "StudentAge" : 20 } {    "_id" : ObjectId("5cf22c11b64a577be5a2bc0d"),    "StudentName" : "David",    "StudentAge" : 19 }Advertisements
 