
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 }
- Related Articles
- How to print to console an object in a MongoDB script?
- How to concatenate results in MongoDB?
- How to terminate a MongoDB shell script earlier?
- How to print NumberLong value in MongoDB?
- How can I use a script to create users in MongoDB?
- How to print document value in MongoDB shell?
- Get output of MongoDB shell script?
- How to echo print statements while executing an SQL script?
- How to get tag count in MongoDB query results based on list of names?
- Program to print its script name as output in Python
- Use result from MongoDB in shell script?
- Script to add a single value to array in MongoDB collection?
- Program to print its script name as output using Python
- Push query results into variable with MongoDB?
- Getting MongoDB results from the previous month\n

Advertisements