
- 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 Aggregate group multiple result?
To aggregate multiple result, use $group in MongoDB. Let us create a collection with documents −
> db.demo765.insertOne( ... ... { ... Name:"John", ... "Category":"ComputerScience", ... "SubjectName":"MongoDB", ... "Marks":75 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054525637cd592b2a4b01") } > > db.demo765.insertOne( ... { ... Name:"John", ... "Category":"ComputerScience", ... "SubjectName":"MySQL", ... "Marks":85 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054525637cd592b2a4b02") } > db.demo765.insertOne( ... { ... Name:"Chris", ... "Category":"10th", ... "SubjectName":"Math", ... "Marks":98 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054535637cd592b2a4b03") }
Display all documents from a collection with the help of find() method −
> db.demo765.find();
This will produce the following output −
{ "_id" : ObjectId("5eb054525637cd592b2a4b01"), "Name" : "John", "Category" : "ComputerScience", "SubjectName" : "MongoDB", "Marks" : 75 } { "_id" : ObjectId("5eb054525637cd592b2a4b02"), "Name" : "John", "Category" : "ComputerScience", "SubjectName" : "MySQL", "Marks" : 85 } { "_id" : ObjectId("5eb054535637cd592b2a4b03"), "Name" : "Chris", "Category" : "10th", "SubjectName" : "Math", "Marks" : 98 }
Following is the query to aggregate group multiple result −
> db.demo765.aggregate([ ... { "$facet": { ... "ListOfName": [ ... { "$group": { ... "_id": "$Name", ... "Marks": { "$sum": "$Marks" } ... }}, ... { "$project": { ... "_id": false, ... "Name": "$_id", ... "Marks": 1 ... }} ... ], ... "ListOfCategory": [ ... { "$group": { ... "_id": "$Category", ... "Marks": { "$sum": "$Marks" } ... }}, ... { "$project": { ... "_id": false, ... "Category": "$_id", ... "Marks": 1 ... }} ... ] ... }} ... ]).pretty()
This will produce the following output −
{ "ListOfName" : [ { "Marks" : 98, "Name" : "Chris" }, { "Marks" : 160, "Name" : "John" } ], "ListOfCategory" : [ { "Marks" : 98, "Category" : "10th" }, { "Marks" : 160, "Category" : "ComputerScience" } ] }
- Related Articles
- Implement MongoDB Aggregate - unwind, group and project?
- To Aggregate Totals in One Group with MongoDB
- Aggregate multiple arrays into one huge array with MongoDB?
- Retrieving group by result with arrays in MongoDB?
- How can I aggregate collection and group by field count in MongoDB?
- MongoDB aggregate to convert multiple documents into single document with an array?
- Aggregate by country, state and city in a MongoDB collection with multiple documents
- Get substring in MongoDB aggregate
- MongoDB aggregate query to sort
- Using $redact in MongoDB aggregate?
- Split a string during MongoDB aggregate
- MongoDB query to implement aggregate function
- MongoDB query to aggregate nested array
- How to aggregate array documents in MongoDB?
- How to use MongoDB Aggregate to sort?

Advertisements