
- 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
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" }
- Related Articles
- MongoDB query for exact match on multiple document fields
- Display MongoDB records by ObjectId?
- Display MongoDB with document and subdocument example and update
- MongoDB query select and display only a specific field from the document?
- How to reach subdata in MongoDB and display a particular document?
- MongoDB exact array matching
- MongoDB query to group records and display a specific value with dot notation
- MongoDB query to skip first 5 records and display only the last 5 of them
- How do I get email-id from a MongoDB document and display with print()
- MongoDB query for exact match
- MongoDB regex to display records whose first five letters are uppercase?
- Display only the employee names with specific salaries in MongoDB documents with employee records?
- Updating a MongoDB document and adding new keys only in the first document?
- Ignore NULL and UNDEFINED values while running a MongoDB query
- Find and display duplicate records in MySQL?

Advertisements