
- 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
MongoDB aggregation framework with group query example?
For this, use $group in MongoDB aggregation. Let us create a collection with documents −
> db.demo639.insertOne( ... { ... "_id" : 1, ... "CountryName" : "US", ... "Info1" : { ... "Name" : "Chris", ... "SubjectName" : "MySQL", ... "Marks" : 78 ... }, ... "Info2" : { ... "Name" : "Chris", ... "SubjectName" : "MySQL", ... "Marks" : 78 ... } ... } ... ); { "acknowledged" : true, "insertedId" : 1 } > db.demo639.insertOne( ... { ... "_id" : 2, ... "CountryName" : "UK", ... "Info1" : { ... "Name" : "Chris", ... "SubjectName" : "MySQL", ... "Marks" : 79 ... }, ... "Info2" : { ... "Name" : "Chris", ... "SubjectName" : "MySQL", ... "Marks" : 88 ... } ... } ... ); { "acknowledged" : true, "insertedId" : 2 }
Display all documents from a collection with the help of find() method −
> db.demo639.find();
This will produce the following output −
{ "_id" : 1, "CountryName" : "US", "Info1" : { "Name" : "Chris", "SubjectName" : "MySQL", "Marks" : 78 }, "Info2" : { "Name" : "Chris", "SubjectName" : "MySQL", "Marks" : 78 } } { "_id" : 2, "CountryName" : "UK", "Info1" : { "Name" : "Chris", "SubjectName" : "MySQL", "Marks" : 79 }, "Info2" : { "Name" : "Chris", "SubjectName" : "MySQL", "Marks" : 88 } }
Following is the query for aggregation framework with $group query −
> db.demo639.aggregate({$group : {_id : {Name1:"$Info1.Name",Name2:"$Info2.Name"}, ... ... Marks:{$first:"$Info2.Marks"}, ... Marks:{$sum:"$Info2.Marks"} ... }}).pretty();
This will produce the following output −
{ "_id" : { "Name1" : "Chris", "Name2" : "Chris" }, "Marks" : 166 }
- Related Articles
- MongoDB query to group several fields using aggregation framework?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB query (aggregation framework) to match a specific field value
- Sort and Group in one MongoDB aggregation query?
- MongoDB query to replace value with aggregation?
- MongoDB aggregation framework match OR is possible?
- Match between fields in MongoDB aggregation framework?
- Does aggregation query with $match works in MongoDB?
- MongoDB aggregation framework to sort by length of array?
- MongoDB aggregation group and remove duplicate array values?
- How to group nested fields in MongoDB aggregation with count value in array?
- MongoDB aggregation with multiple keys
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- Get the average of an entire field using the aggregation framework in MongoDB?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?

Advertisements