
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Display the undefined and exact MongoDB document records
For this, use the forEach(). To display the values, use printjson(). Let us create a collection with documents −
> db.demo496.insertOne({"Name":"David","CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b04ab0f3fa88e22790ce") } > db.demo496.insertOne({"Name":"John","CountryName":"AUS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b054b0f3fa88e22790cf") } > db.demo496.insertOne({"Name":"Robert","CountryName":"UK"});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b05db0f3fa88e22790d0") }
Display all documents from a collection with the help of find() method −
> db.demo496.find();
This will produce the following output −
{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" } { "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : "Robert", "CountryName" : "UK" }
Following is the query to display undefined for documents −
> db.demo496.find({}).forEach( (done, notDone) => { printjson(notDone); });
This will produce the following output −
undefined undefined undefined
Following is the query to display the documents with.forEach() −
> db.demo496.find({}).forEach( (done, notDone) => { printjson(done); });
This will produce the following output −
{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" } { "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : "Robert", "CountryName" : "UK" }
Advertisements