
- 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
How to retrieve all nested fields from MongoDB collection?
For this, use aggregate(). Let us create a collection with documents −
>db.demo138.insertOne({"Id":101,"PlayerDetails":[{"PlayerName":"Chris","PlayerScore":400},{"PlayerName":"David","PlayerScore":1000}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31bb9ffdf09dd6d08539a1") } >db.demo138.insertOne({"Id":102,"PlayerDetails":[{"PlayerName":"Bob","PlayerScore":500},{"PlayerName":"Carol","PlayerScore":600}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31bbcefdf09dd6d08539a2") }
Display all documents from a collection with the help of find() method −
> db.demo138.find();
This will produce the following output −
{ "_id" : ObjectId("5e31bb9ffdf09dd6d08539a1"), "Id" : 101, "PlayerDetails" : [ { "PlayerName" : "Chris", "PlayerScore" : 400 }, { "PlayerName" : "David", "PlayerScore" : 1000 } ] } { "_id" : ObjectId("5e31bbcefdf09dd6d08539a2"), "Id" : 102, "PlayerDetails" : [ { "PlayerName" : "Bob", "PlayerScore" : 500 }, { "PlayerName" : "Carol", "PlayerScore" : 600 } ] }
Following is the query to retrieve all nested fields from the collection −
> db.demo138.aggregate([{ $unwind:"$PlayerDetails" }, { $project: { "PlayerName":"$PlayerDetails.PlayerName", "PlayerScore":"$PlayerDetails.PlayerScore" } } ] );
This will produce the following output −
{ "_id" : ObjectId("5e31bb9ffdf09dd6d08539a1"), "PlayerName" : "Chris", "PlayerScore" : 400 } { "_id" : ObjectId("5e31bb9ffdf09dd6d08539a1"), "PlayerName" : "David", "PlayerScore" : 1000 } { "_id" : ObjectId("5e31bbcefdf09dd6d08539a2"), "PlayerName" : "Bob", "PlayerScore" : 500 } { "_id" : ObjectId("5e31bbcefdf09dd6d08539a2"), "PlayerName" : "Carol", "PlayerScore" : 600 }
- Related Articles
- How to retrieve all the documents from a MongoDB collection using Java?
- How to retrieve documents from a collection in MongoDB?
- Retrieve data from a MongoDB collection?
- Get all fields names in a MongoDB collection?
- Retrieve values from nested JSON array in MongoDB?
- How to retrieve a nested object in MongoDB?
- How to re-map the fields of a MongoDB collection?
- MongoDB query to retrieve records from a collection named with letters and numbers
- How to delete all the documents from a collection in MongoDB?
- MongoDB collection query to exclude some fields in find()?
- How to group nested fields in MongoDB aggregation with count value in array?
- How to remove duplicates from MongoDB Collection?
- MongoDB query to get only specific fields in nested array documents?
- How to find datatype of all the fields in MongoDB?
- How to remove all documents from a collection except a single document in MongoDB?

Advertisements