- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 document value in MongoDB shell?
For this, work with the concept of forEach(). Let us first create a collection with documents −
> db.printDocuementValueDemo.insertOne({"InstructorName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6804f7924bb85b3f48950") } > db.printDocuementValueDemo.insertOne({"InstructorName":"Sam Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd680577924bb85b3f48951") } > db.printDocuementValueDemo.insertOne({"InstructorName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd680637924bb85b3f48952") }
Following is the query to display all documents from a collection with the help of find() method −
> db.printDocuementValueDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd6804f7924bb85b3f48950"), "InstructorName" : "John Smith" } { "_id" : ObjectId("5cd680577924bb85b3f48951"), "InstructorName" : "Sam Williams" } { "_id" : ObjectId("5cd680637924bb85b3f48952"), "InstructorName" : "David Miller" }
Following is the query to print document value in MongoDB shell −
> db.printDocuementValueDemo.find( { _id : ObjectId("5cd680577924bb85b3f48951") }, {InstructorName: 1, _id:0} ).forEach(function(myDocument) { print(myDocument.InstructorName); });
This will produce the following output −
Sam Williams
- Related Articles
- How to print NumberLong value in MongoDB?
- Format date value in MongoDB shell?
- Iterate a cursor and print a document in MongoDB?
- Perform nested document value search in MongoDB?
- How to update document with marks value in MongoDB for student David
- How to get items with a specific value from documents using MongoDB shell?
- How to unset a variable in MongoDB shell?
- How to define aliases in the MongoDB Shell?
- Increment only a single value in MongoDB document?
- How do I delete array value from a document in MongoDB?
- How to replace substring in MongoDB document?
- How to delete a document in MongoDB?
- How to add a sub-document to sub-document array in MongoDB?
- In MongoDB how do you use $set to update a nested value/embedded document?
- Update only a specific value in a MongoDB document

Advertisements