
- 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
Iterate a cursor and print a document in MongoDB?
For this, use printjson. Let us first create a collection with documents −
> db.cursorDemo.insertOne({"StudentFullName":"John Smith","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0d08f9e6ff3eb0ce442") } > db.cursorDemo.insertOne({"StudentFullName":"John Doe","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0df8f9e6ff3eb0ce443") } > db.cursorDemo.insertOne({"StudentFullName":"Carol Taylor","StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444") } > db.cursorDemo.insertOne({"StudentFullName":"Chris Brown","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0f88f9e6ff3eb0ce445") }
Following is the query to display all documents from a collection with the help of find() method −
> db.cursorDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"), "StudentFullName" : "John Smith", "StudentAge" : 23 } { "_id" : ObjectId("5cc7f0df8f9e6ff3eb0ce443"), "StudentFullName" : "John Doe", "StudentAge" : 21 } { "_id" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444"), "StudentFullName" : "Carol Taylor", "StudentAge" : 20 } { "_id" : ObjectId("5cc7f0f88f9e6ff3eb0ce445"), "StudentFullName" : "Chris Brown", "StudentAge" : 24 }
Following is the query to iterate and print document with printjson −
> db.cursorDemo.find().forEach(printjson);
This will produce the following output −
{ "_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"), "StudentFullName" : "John Smith", "StudentAge" : 23 } { "_id" : ObjectId("5cc7f0df8f9e6ff3eb0ce443"), "StudentFullName" : "John Doe", "StudentAge" : 21 } { "_id" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444"), "StudentFullName" : "Carol Taylor", "StudentAge" : 20 } { "_id" : ObjectId("5cc7f0f88f9e6ff3eb0ce445"), "StudentFullName" : "Chris Brown", "StudentAge" : 24 }
Following is the second query if we want the specific fields only like the fields “StudentFullName” and “StudentAge” −
> db.cursorDemo.find({}, { "StudentFullName": 1,"StudentAge":1, "_id": 0 }).forEach(printjson)
This will produce the following output −
{ "StudentFullName" : "John Smith", "StudentAge" : 23 } { "StudentFullName" : "John Doe", "StudentAge" : 21 } { "StudentFullName" : "Carol Taylor", "StudentAge" : 20 } { "StudentFullName" : "Chris Brown", "StudentAge" : 24 }
- Related Articles
- How to print document value in MongoDB shell?
- How do I get email-id from a MongoDB document and display with print()
- Updating a MongoDB document and adding new keys only in the first document?
- Find a document with ObjectID in MongoDB?
- Remove only a single document in MongoDB
- How to delete a document in MongoDB?
- Update only a single document in MongoDB
- Finding a MongoDB document through a word
- Update a MongoDB document with Student Id and Name
- MongoDB “$and” operator for subcollection to fetch a document?
- Update only a specific value in a MongoDB document
- How to add a sub-document to sub-document array in MongoDB?
- How to reach subdata in MongoDB and display a particular document?
- Return True if a document exists in MongoDB?
- Retrieving the first document in a MongoDB collection?

Advertisements