
- 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 can I aggregate nested documents in MongoDB?
To aggregate nested documents in MongoDB, you can use $group. Let us first create a collection with documents −
> db.aggregateDemo.insertOne( ... { ... "ProductInformation": [ ... { ... "Product1": [ ... { ... Amount: 50 ... }, ... { ... Amount: 90 ... }, ... { ... Amount: 30 ... } ... ] ... }, ... { ... "Product1": [ ... { ... Amount: 200 ... }, ... { ... Amount: 30 ... }, ... { ... Amount: 40 ... } ... ] ... }, ... { ... "Product1": [ ... { ... Amount: 150 ... }, ... { ... Amount: 190 ... }, ... { ... Amount: 198 ... } ... ] ... } ... ... ] ... }); { "acknowledged" : true, "insertedId" : ObjectId("5e04df58150ee0e76c06a04d") } > db.aggregateDemo.insertOne( ... { ... "ProductInformation": [ ... { ... "Product1": [ ... { ... Amount: 100 ... }, ... { ... Amount: 1002 ... }, ... { ... Amount: 78 ... } ... ] ... }, ... { ... "Product1": [ ... { ... Amount: 75 ... }, ... { ... Amount: 400 ... }, ... { ... Amount: 600 ... } ... ] ... }, ... { ... "Product1": [ ... { ... Amount: 700 ... }, ... { ... Amount: 500 ... }, ... { ... Amount: 600 ... } ... ] ... } ... ... ] ... }); { "acknowledged" : true, "insertedId" : ObjectId("5e04df93150ee0e76c06a04e") }
Following is the query to display all documents from a collection with the help of find() method −
> db.aggregateDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e04df58150ee0e76c06a04d"), "ProductInformation" : [ { "Product1" : [ { "Amount" : 50 }, { "Amount" : 90 }, { "Amount" : 30 } ] }, { "Product1" : [ { "Amount" : 200 }, { "Amount" : 30 }, { "Amount" : 40 } ] }, { "Product1" : [ { "Amount" : 150 }, { "Amount" : 190 }, { "Amount" : 198 } ] } ] } { "_id" : ObjectId("5e04df93150ee0e76c06a04e"), "ProductInformation" : [ { "Product1" : [ { "Amount" : 100 }, { "Amount" : 1002 }, { "Amount" : 78 } ] }, { "Product1" : [ { "Amount" : 75 }, { "Amount" : 400 }, { "Amount" : 600 } ] }, { "Product1" : [ { "Amount" : 700 }, { "Amount" : 500 }, { "Amount" : 600 } ] } ] }
Here is the query to aggregate nested documents −
> db.aggregateDemo.aggregate([ ... { ... $unwind:"$ProductInformation" ... }, ... { ... $unwind:"$ProductInformation.Product1" ... }, ... { ... $group:{ ... _id:null, ... MaximumAmount:{ ... $max:"$ProductInformation.Product1.Amount" ... } ... } ... } ... ]);
This will produce the following output −
{ "_id" : null, "MaximumAmount" : 1002 }
- Related Articles
- How to aggregate array documents in MongoDB?
- MongoDB query to aggregate nested array
- Aggregation in MongoDB for nested documents?
- Updating Nested Embedded Documents in MongoDB?
- How can I search a collection to find a nested value in one of its documents in MongoDB?
- How can I aggregate collection and group by field count in MongoDB?
- How can I rename a field for all documents in MongoDB?
- How to display only the keys from nested MongoDB documents?
- Update objects in a MongoDB documents array (nested updating)?
- Find in MongoDB documents with filled nested array and reshape the documents result
- Can I retrieve multiple documents from MongoDB by id?
- Aggregate based on array value to sum values in different MongoDB documents?
- How can I sort documents in MongoDB 4 and display only a single field?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB Aggregate sum of values in a list of dictionaries for all documents?

Advertisements