
- 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
Is there any way to see the MongoDB results in a better format?
Yes, you can use the findOne(). Following is the syntax −
db.yourCollectionName.findOne();
You can use toArray() as well −
db.yourCollectionName.find().toArray();
Let us first create a collection with documents −
> db.betterFormatDemo.insertOne({"StudentName":"Adam Smith","StudentScores":[98,67,89]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ab826d78f205348bc640") } > db.betterFormatDemo.insertOne({"StudentName":"John Doe","StudentScores":[67,89,56]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ab936d78f205348bc641") } > db.betterFormatDemo.insertOne({"StudentName":"Sam Williams","StudentScores":[45,43,33]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7aba76d78f205348bc642") }
Following is the query to display all documents from a collection with the help of find() method −
> db.betterFormatDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd7ab826d78f205348bc640"), "StudentName" : "Adam Smith", "StudentScores" : [ 98, 67, 89 ] } { "_id" : ObjectId("5cd7ab936d78f205348bc641"), "StudentName" : "John Doe", "StudentScores" : [ 67, 89, 56 ] } { "_id" : ObjectId("5cd7aba76d78f205348bc642"), "StudentName" : "Sam Williams", "StudentScores" : [ 45, 43, 33 ] }
Case 1 − Using findOne().
Following is the query to display the MongoDB results in a better format −
> db.betterFormatDemo.findOne();
This will produce the following output −
{ "_id" : ObjectId("5cd7ab826d78f205348bc640"), "StudentName" : "Adam Smith", "StudentScores" : [ 98, 67, 89 ] }
Case 2 − Using find().toArray().
Following is the query to display the MongoDB results in a better format −
> db.betterFormatDemo.find().toArray();
This will produce the following output −
[ { "_id" : ObjectId("5cd7ab826d78f205348bc640"), "StudentName" : "Adam Smith", "StudentScores" : [ 98, 67, 89 ] }, { "_id" : ObjectId("5cd7ab936d78f205348bc641"), "StudentName" : "John Doe", "StudentScores" : [ 67, 89, 56 ] }, { "_id" : ObjectId("5cd7aba76d78f205348bc642"), "StudentName" : "Sam Williams", "StudentScores" : [ 45, 43, 33 ] } ]
- Related Articles
- Is there any way to skip some documents in MongoDB?
- Is there any way in MongoDB to get the inner value of json data?
- Is there a way to change the date format in php?
- Is there a way to list collections in MongoDB?
- PHP: is there a way to see “invisible” characters like \n
- Is there a way to limit the number of records in a certain MongoDB collection?
- Is there any way to check if there is a null value in an object or array in JavaScript?
- Is there any easy way to add multiple records in a single MySQL query?
- Is there any way to embed a PDF file into an HTML5 page?
- Is there any way to load an extension in chrome browser using Selenium Webdriver?
- Is there any way to use values from a JSON object in a MySQL Select statement?\n
- Is there any more efficient way to code this “2 Sum” Questions JavaScript
- Is there any way I can call the validate() function outside the initValidation() function in JavaScript?
- Is there any way of moving to HTML 5 and still promise multi browser compatibility?
- Is there a way to know your current username in MySQL?

Advertisements